如何使用pandas重新采样从CSV加载的日期列?

时间:2016-01-09 20:19:00

标签: python pandas resampling

我从CSV加载了这些数据。

        Date                        X           Y           Z
    0   2015-11-30 20:23:05.556     281.764900  -43.895060  8.714666
    1   2015-11-30 20:23:05.757     192.519990  -44.636436  1.720552
    2   2015-11-30 20:23:05.958     149.030600  -45.098050  1.958352
    3   2015-11-30 20:23:06.171     140.707600  -44.622448  1.510729
    4   2015-11-30 20:23:06.366     139.154890  -45.154003  4.783974
    5   2015-11-30 20:23:06.564     138.875140  -44.790306  2.266093
    6   2015-11-30 20:23:06.766     138.357570  -44.048930  4.210457
    7   2015-11-30 20:23:06.967     136.846830  -45.909367  -2.196152
    8   2015-11-30 20:23:07.168     137.322430  -45.126026  0.139882
    9   2015-11-30 20:23:07.369     137.322430  -45.349840  0.587506
    10  2015-11-30 20:23:07.573     132.552460  -48.455223  5.259574

列的dtypes是:

Date    datetime64[ns]
X              float64
Y              float64
Z              float64
dtype: object

我想将Date列重新采样到例如100毫秒。我试着用

something.unstack().Date.resample('100L').Date.stack()

但它写错了

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex

你知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

df.index = pd.to_datetime(df['Date'], format='%Y-%m-%d %H:%M:%S.%f')
df = df.drop('Date', axis=1)
df = df.resample('resamplestring', how='mean')