Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove timezone from a Timestamp column in a pandas dataframe

I read Pandas change timezone for forex DataFrame but I'd like to make the time column of my dataframe timezone naive for interoperability with an sqlite3 database.

The data in my pandas dataframe is already converted to UTC data, but I do not want to have to maintain this UTC timezone information in the database.

Given a sample of the data derived from other sources, it looks like this:

print(type(testdata)) print(testdata) print(testdata.applymap(type)) 

gives:

<class 'pandas.core.frame.DataFrame'>                         time  navd88_ft  station_id  new 0  2018-03-07 01:31:02+00:00  -0.030332          13    5 1  2018-03-07 01:21:02+00:00  -0.121653          13    5 2  2018-03-07 01:26:02+00:00  -0.072945          13    5 3  2018-03-07 01:16:02+00:00  -0.139917          13    5 4  2018-03-07 01:11:02+00:00  -0.152085          13    5                                      time        navd88_ft     station_id  \ 0  <class 'pandas._libs.tslib.Timestamp'>  <class 'float'>  <class 'int'>    1  <class 'pandas._libs.tslib.Timestamp'>  <class 'float'>  <class 'int'>    2  <class 'pandas._libs.tslib.Timestamp'>  <class 'float'>  <class 'int'>    3  <class 'pandas._libs.tslib.Timestamp'>  <class 'float'>  <class 'int'>    4  <class 'pandas._libs.tslib.Timestamp'>  <class 'float'>  <class 'int'>                  new   0  <class 'int'>   1  <class 'int'>   2  <class 'int'>   3  <class 'int'>   4  <class 'int'>   

but

newstamp = testdata['time'].tz_convert(None) 

gives an eventual error:

TypeError: index is not a valid DatetimeIndex or PeriodIndex 

What do I do to replace the column with a timezone naive timestamp?

like image 456
Dave X Avatar asked Mar 09 '18 16:03

Dave X


People also ask

How do I remove timezone from datetime?

To remove timestamp, tzinfo has to be set None when calling replace() function. First, create a DateTime object with current time using datetime. now(). The DateTime object was then modified to contain the timezone information as well using the timezone.

What is TZ-naive and TZ aware?

Localize tz-naive DatetimeIndex to tz-aware DatetimeIndex. This method takes a time zone (tz) naive DatetimeIndex object and makes this time zone aware. It does not move the time to another time zone. Time zone localization helps to switch from time zone aware to time zone unaware objects.


1 Answers

The column must be a datetime dtype, for example after using pd.to_datetime. Then, you can use tz_localize to change the time zone, a naive timestamp corresponds to time zone None:

testdata['time'].dt.tz_localize(None) 

Unless the column is an index (DatetimeIndex), the .dt accessor must be used to access pandas datetime functions.

like image 76
MaFF Avatar answered Oct 10 '22 06:10

MaFF