pandas 如何从熊猫数据框中的时间戳列中删除时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49198068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove timezone from a Timestamp column in a pandas dataframe
提问by Dave X
I read Pandas change timezone for forex DataFramebut I'd like to make the time column of my dataframe timezone naive for interoperability with an sqlite3 database.
我读了Pandas change timezone for forex DataFrame,但我想让我的数据帧时区的时间列幼稚,以便与 sqlite3 数据库进行互操作。
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.
我的 Pandas 数据帧中的数据已经转换为 UTC 数据,但我不想在数据库中维护这个 UTC 时区信息。
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?
我该怎么做才能用时区天真时间戳替换该列?
回答by MaFF
You can use tz_localize
to change the time zone, a naive timestamp corresponds to time zone None
:
您可以使用tz_localize
来更改时区,一个朴素的时间戳对应于时区None
:
testdata['time'].dt.tz_localize(None)
Unless the column is an index you have to call method dt
to access pandas datetime functions.
除非该列是索引,否则您必须调用方法dt
来访问 Pandas 日期时间函数。