Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法转换数据帧时间戳_Python_Indexing_Pandas_Timezone_Timestamp - Fatal编程技术网

Python 无法转换数据帧时间戳

Python 无法转换数据帧时间戳,python,indexing,pandas,timezone,timestamp,Python,Indexing,Pandas,Timezone,Timestamp,我对熊猫的工作还很陌生,我想弄明白为什么这个时间戳不能转换。例如,一个单独的时间戳是字符串'2010-10-06 16:38:02'。代码如下所示: newdata = pd.DataFrame.from_records(data, columns = ["col1", "col2", "col3", "timestamp"], index = "timestamp") newdata.index = newdata.index.tz_localize('UTC').tz_convert('US

我对熊猫的工作还很陌生,我想弄明白为什么这个时间戳不能转换。例如,一个单独的时间戳是字符串
'2010-10-06 16:38:02'
。代码如下所示:

newdata = pd.DataFrame.from_records(data, columns = ["col1", "col2", "col3", "timestamp"], index = "timestamp")
newdata.index = newdata.index.tz_localize('UTC').tz_convert('US/Eastern')
并获取以下错误:

AttributeError: 'Index' object has no attribute 'tz_localize'
有人评论说tz_localize不是一种可用于索引类型的方法,所以我尝试将其转换为列,但这会导致错误

TypeError: index is not a valid DatetimeIndex or PeriodIndex
然后我发现,这表明tz_本地化只对索引起作用

如果有人能帮助我,我将不胜感激!我用的是熊猫0.15.2。我相信这段代码可能已经为其他人使用了早期版本,但我无法切换

编辑:


好的,经过一番讨论之后,我发现这不会引发任何错误,而且似乎在短期内实现了我想要的功能:
newdata.index=pd.DatetimeIndex(newdata.index).tz_localize('UTC').tz_convert('US/‌​东部)

您必须首先将索引列转换为pandas中的系列类型:

newdata.index.to_series().tz_localize('UTC').tz_convert('US/Eastern')

我被要求添加一个正式的答案,而不仅仅是编辑我的问题,所以就在这里。注意,它建立在上述答案的基础上,但这个答案对我来说并不太合适


newdata.index=pd.DatetimeIndex(newdata.index).tz_本地化('UTC').tz_转换('US')/‌​

艾琳,请你添加一个答案并将问题标记为已解决,好吗?否则,它会弹出一个未回答的问题,让人困惑……当然,没问题。这不是一个简单的方法@艾琳提到了这一点,这就是(我的)方法。