Can I export pandas DataFrame to Excel stripping tzinfo?

2024/10/15 19:28:04

I have a timezone aware TimeSeries in pandas 0.10.1. I want to export to Excel, but the timezone prevents the date from being recognized as a date in Excel.

In [40]: resultado
Out[40]: 
fecha_hora
2013-04-11 13:00:00+02:00    31475.568
2013-04-11 14:00:00+02:00    37263.072
2013-04-11 15:00:00+02:00    35979.434
2013-04-11 16:00:00+02:00    35132.890
2013-04-11 17:00:00+02:00    36356.584

If I strip the tzinfo with .tz_convert(None), the date gets converted to UTC:

In [41]: resultado.tz_convert(None)
Out[41]: 
fecha_hora
2013-04-11 11:00:00    31475.568
2013-04-11 12:00:00    37263.072
2013-04-11 13:00:00    35979.434
2013-04-11 14:00:00    35132.890
2013-04-11 15:00:00    36356.584

Is there a TimeSeries method to apply .replace(tzinfo=None) to each date in the index?

Alternativelly, is there a way to properly export time-aware TimeSeries to Excel?

Answer

You can simply create a copy without timezone.

import pandas as patime = pa.Timestamp('2013-04-16 10:08', tz='Europe/Berlin')
time_wo_tz = pa.datetime(year=time.year, month=time.month, day=time.day, hour=time.hour, minute=time.minute, second=time.second,microsecond=time.microsecond)

When you want to convert the whole index of the timeseries, use a list comprehension.

ts.index = [pa.datetime(year=x.year, month=x.month, day=x.day, hour=x.hour, minute=x.minute, second=x.second, microsecond=x.microsecond) for x in ts.index]
https://en.xdnf.cn/q/117795.html

Related Q&A

Converting kwargs into variables?

How do I convert kwargs objects into local variables? I am a math teacher and I want to write a helper function where I can use JS-style template strings to write problems.I want to do something like …

Maya Python: Unbound Method due to Reload()

I have two files that import the same object tracking method from a third file. It works something like thisfile TrackingMethodclass Tracker(object):def __init__(self,nodeName=None,dag=None,obj=None):#…

How to append to a table in BigQuery using Python BigQuery API

Ive been able to append/create a table from a Pandas dataframe using the pandas-gbq package. In particular using the to_gbq method. However, When I want to check the table using the BigQuery web UI I s…

Splitting values out of a CSV Reader Python

Here is my current code a_reader = None a_reader = open(data.csv, rU) a_csv_reader = csv.reader(a_reader)for row in a_csv_reader:print row a_reader.close()count = 0 sum = 0.0 a_reader = open(…

python - list variable not storing proper result in recursion

I am trying to store all possible parenthesisation of a list through recursion.Example: printRange(0,3) Answer will be [[0],[1], [2]], [[0], [1, 2]], [[0, 1], [2]], [[0, 1, 2]]I could get a right answe…

Embedding matplotlib canvas into tkinter GUI - plot is not showing up, but no error is thrown

Running the python python script below does not show the embedded matplotlib plot. However it also throws no error message. Upon running the script, it is supposed to display a GUI displaying 4 buttons…

Can I get rid of this b character in my print statement? [duplicate]

This question already has answers here:What does a b prefix before a python string mean?(2 answers)Closed 7 years ago.Im wondering what this b charcter is and why its appearing. Im also wondering if I…

Python tkinter: configure multiple labels with a loop

I have a window with multiple labels. Instead of configuring each label individually, I want to use a for loop to configure them.Basically, what I get from the below code is all labels are showing the …

How do you create a sitemap index in Django?

The Django documentation is very minimal and I cant seem to get this to work.Currently I have 3 individual sitemaps, and I would like to create a sitemap index for them: (r^sitemap1\.xml$, django.contr…

Numpy Append to Array

I’m building a project for the Raspberry Pi that turns a relay on and off random times in a specific time window. To manage the time slots, I want to use a two-dimensional array that’s generated dail…