Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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 tz感知str到datetime对象以进行重采样计算 我有以下几个字符串 我想在此数据上使用data.resample('D')操作 我在把它转换成正确的格式时遇到了一些困难。我曾尝试使用pd.to_datetime,但在尝试使用重采样时出现以下错误_Python_Pandas_Datetime - Fatal编程技术网

Python tz感知str到datetime对象以进行重采样计算 我有以下几个字符串 我想在此数据上使用data.resample('D')操作 我在把它转换成正确的格式时遇到了一些困难。我曾尝试使用pd.to_datetime,但在尝试使用重采样时出现以下错误

Python tz感知str到datetime对象以进行重采样计算 我有以下几个字符串 我想在此数据上使用data.resample('D')操作 我在把它转换成正确的格式时遇到了一些困难。我曾尝试使用pd.to_datetime,但在尝试使用重采样时出现以下错误,python,pandas,datetime,Python,Pandas,Datetime,TypeError:仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了“Index”的实例 >>> test1['timestamp'] 0 2017-01-03 08:30:00-06:00 1 2017-01-03 08:30:32-06:00 2 2017-01-03 08:30:42-06:00 3 2017-01-03 08:30:46-06:00 4

TypeError:仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了“Index”的实例

>>> test1['timestamp']
0        2017-01-03 08:30:00-06:00
1        2017-01-03 08:30:32-06:00
2        2017-01-03 08:30:42-06:00
3        2017-01-03 08:30:46-06:00
4        2017-01-03 08:30:52-06:00
                   ...            
65334    2017-12-29 14:55:02-06:00
65335    2017-12-29 14:55:26-06:00
65336    2017-12-29 14:55:54-06:00
65337    2017-12-29 14:59:23-06:00
65338    2017-12-29 14:59:46-06:00
Name: timestamp, Length: 65339, dtype: object
实现这一目标的最佳方式是什么


谢谢

您的错误消息以仅对日期时间索引有效开始

所以,在将时间戳列转换为datetime之后,创建 其中的DatetimeIndex(并保存以备将来使用):

结果是:

DatetimeIndex(['2017-01-03 08:30:00-06:00', '2017-01-03 08:30:32-06:00',
               '2017-01-03 08:30:42-06:00', '2017-01-03 08:30:46-06:00',
               '2017-01-03 08:30:52-06:00'],
              dtype='datetime64[ns, pytz.FixedOffset(-360)]', name='timestamp', freq=None)
或者,您应该在数据帧中设置索引,基于 在上述公式中:

wrk = df.set_index(pd.DatetimeIndex(pd.to_datetime(df.timestamp)))\
    .drop(columns=['timestamp'])
然后对其重新编制索引(使用一些聚合函数),例如:

我将源df创建为:

并得到如下结果:

                           amount
timestamp                        
2017-01-03 08:30:00-06:00      12
2017-01-03 08:30:15-06:00       0
2017-01-03 08:30:30-06:00      31
2017-01-03 08:30:45-06:00      42

是否尝试将pd.to_datetime从obj(字符串)转换为datetime数据类型?
wrk.resample('15s').sum()
                   timestamp  amount
0  2017-01-03 08:30:00-06:00      12
1  2017-01-03 08:30:32-06:00      14
2  2017-01-03 08:30:42-06:00      17
3  2017-01-03 08:30:46-06:00      19
4  2017-01-03 08:30:52-06:00      23
                           amount
timestamp                        
2017-01-03 08:30:00-06:00      12
2017-01-03 08:30:15-06:00       0
2017-01-03 08:30:30-06:00      31
2017-01-03 08:30:45-06:00      42