Python 3.x 熊猫:使用'时处理偏移时区值;DatetimeIndex';
我有一个Python 3.x 熊猫:使用'时处理偏移时区值;DatetimeIndex';,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有一个datetime列EST EST 2017-01-01 19:00:00-05:00 2017-01-01 19:01:00-05:00 2017-01-01 19:02:00-05:00 2017-01-01 19:03:00-05:00 2017-01-01 19:05:00-05:00 2017-01-01 19:06:00-05:00 .... 2017-12-31 23:01:00-05:00 在这里,您可以看到每次都有一个拖尾-05:00。这个尾随的-05:00是在我从GM
datetime
列EST
EST
2017-01-01 19:00:00-05:00
2017-01-01 19:01:00-05:00
2017-01-01 19:02:00-05:00
2017-01-01 19:03:00-05:00
2017-01-01 19:05:00-05:00
2017-01-01 19:06:00-05:00
....
2017-12-31 23:01:00-05:00
在这里,您可以看到每次都有一个拖尾-05:00
。这个尾随的-05:00
是在我从GMT
转换到EST
时添加的
如何删除-05:00
原因是当我这样做时:
df['EST']=pd.DatetimeIndex(df['EST']')
我得到这个错误:
TypeError:[datetime.datetime(2017,12,31,19,0,tzinfo=tzoffset(None,-18000))
您能帮我解决这个问题吗?我们可以通过使用删除
-05:00
,并将其设置为None
。然后我们可以使用pd.\u datetime
:
df['EST'] = pd.to_datetime(df['EST']).dt.tz_localize(None)
如果EST
是您的索引,请使用:
df.index = pd.DatetimeIndex(df['EST']).dt.tz_localize(None)
我们可以使用删除
-05:00
,并将其设置为None
。然后我们可以使用pd.to\u datetime
:
df['EST'] = pd.to_datetime(df['EST']).dt.tz_localize(None)
如果EST
是您的索引,请使用:
df.index = pd.DatetimeIndex(df['EST']).dt.tz_localize(None)
我想您可以使用
.dt.tz_localize
选项None
假设您的EST
列已经是datetime
dtype
样本数据:
df['EST']:
0 2017-01-01 19:00:00-05:00
1 2017-01-01 19:01:00-05:00
2 2017-01-01 19:02:00-05:00
3 2017-01-01 19:03:00-05:00
4 2017-01-01 19:05:00-05:00
5 2017-01-01 19:06:00-05:00
Name: EST, dtype: datetime64[ns, US/Eastern]
使用.dt.tz_本地化输出
df['EST'].dt.tz_localize(None)
Out[2386]:
0 2017-01-01 19:00:00
1 2017-01-01 19:01:00
2 2017-01-01 19:02:00
3 2017-01-01 19:03:00
4 2017-01-01 19:05:00
5 2017-01-01 19:06:00
Name: EST, dtype: datetime64[ns]
我想您可以使用.dt.tz_localize
选项None
假设您的EST
列已经是datetime
dtype
样本数据:
df['EST']:
0 2017-01-01 19:00:00-05:00
1 2017-01-01 19:01:00-05:00
2 2017-01-01 19:02:00-05:00
3 2017-01-01 19:03:00-05:00
4 2017-01-01 19:05:00-05:00
5 2017-01-01 19:06:00-05:00
Name: EST, dtype: datetime64[ns, US/Eastern]
使用.dt.tz_本地化输出
df['EST'].dt.tz_localize(None)
Out[2386]:
0 2017-01-01 19:00:00
1 2017-01-01 19:01:00
2 2017-01-01 19:02:00
3 2017-01-01 19:03:00
4 2017-01-01 19:05:00
5 2017-01-01 19:06:00
Name: EST, dtype: datetime64[ns]
当我执行df['EST'].dt.tz_localize(None)
时,我得到了这个错误AttributeError:只能使用带有datetimelike值的.dt访问器
@floss:这意味着你的df['EST']不是datetime
dtype。您需要使用pd.to\u datetime
将其转换为datetime,或者使用pd.datetimeindex
构建直接datetime索引,并指定tz
信息。但是,最终输出将-05:00
反映到时间中。这意味着它将是00:00:xx
,并且日期增加1天aymydf['EST']
格式正确,但是当我从GMT
转换到EST
时,我得到了尾随偏移量值-05:00
。当我执行df['EST'].dt.tz_本地化(无)
时,我得到了这个错误AttributeError:只能使用带有datetimelike值的.dt访问器
@floss:这意味着你的df['EST']不是datetime
dtype。您需要使用pd.to\u datetime
将其转换为datetime,或者使用pd.datetimeindex
构建直接datetime索引,并指定tz
信息。但是,最终输出将-05:00
反映到时间中。这意味着它将是00:00:xx
,并且日期增加1天aymydf['EST']
格式正确,但是当我从GMT
转换到EST
时,我得到了尾随偏移量值-05:00
。这很有效,但在这种情况下这是最佳解决方案吗?pd.DatetimeIndex(df['EST'])
和pd.to_datetime(df['EST'])之间有什么区别
?因为我一直在使用pd.DatetimeIndex
,所以我假设“EST”在本例中是一列,而不是您的索引。如果它是您的索引,您是正确的,我们必须使用pd.DatetimeIndex
,请参阅我的编辑。@flossI使用了pd.to\u datetime(df['EST']).dt.tz\u本地化(无)
我得到了这个错误:ValueError:Array必须是同一个时区
(和)ValueError:Tz-aware-datetime.datetime无法转换为datetime64,除非utc=True
我可以确认您提供的示例数据集的情况。如果这适用于您的整个数据集,则它可以正常工作。@flossThis可以工作,但在这种情况下,这是最佳解决方案吗?pd.DatetimeIndex(df)之间的区别是什么['EST']'
和pd.to_datetime(df['EST'])
?因为我一直在使用pd.DatetimeIndex
,所以我假设在这种情况下'EST'是一列,而不是你的索引。如果它是你的索引,你是正确的,我们必须使用pd.DatetimeIndex
,请参见我的编辑。@flossI usedpd.to_datetime(df['EST']).dt\u本地化(无)
我得到了这个错误:ValueError:Array必须都是相同的时区
(并且)ValueError:Tz-aware-datetime.datetime不能转换为datetime64,除非utc=True
我可以确认您提供的示例数据集。如果这可以推广到整个数据集,那么它就可以正常工作。@floss