【问题标题】:Python: Assigning timestamps to dataframe using separate date and time values in datasetPython:使用数据集中单独的日期和时间值将时间戳分配给数据帧
【发布时间】:2014-07-02 11:42:08
【问题描述】:

我使用df=pd.to_csv(file, header = 0) 加载了一个csv,我有两列DateTime,格式如下: 20120718 (yyyymmdd) 和 15:59:56.319000 (H:M:S:MS)。

如何将两者结合起来,以便使用此日期和时间戳列设置我的数据框 df 索引并拥有正确的索引时间序列?

【问题讨论】:

    标签: python pandas timestamp time-series


    【解决方案1】:
    df.sort_index(by=['Date', 'Time'], inplace=True)
    df.index = np.arange(1, len(df) + 1)
    

    是最简单的方法。如果你真的想要一个时间序列作为索引,那么:

    df['timeseries'] = pd.to_datetime(df.Date + ' ' + df.Time, unit='ms')
    

    【讨论】:

    • 谢谢,但 df.Date 在int。所以我使用上面的代码:df['timeseries'] = pd.to_datetime(df.Date + ' ' + df.Time, unit='ms') 并将str 添加到 df.Date 它搞砸了一切。
    • 你说得对,它不能作为 int 工作。我还以为是一根绳子。试试(df.Date.astype(str) + ' ' + df.Time, unit='ms')
    猜你喜欢
    • 1970-01-01
    • 2016-12-07
    • 2021-12-02
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多