【问题标题】:combine the date and time to create an index in times series结合日期和时间创建时间序列索引
【发布时间】:2016-10-11 06:44:08
【问题描述】:

我加载数据

  df3=sql.read_sql("select * from referenceMesure",conn)
  print df3

输出

                NumeroM Date      Heure          Reg 
          0       2396 2016-05-26   15:32:00     2396           
          1       2397 2016-05-26   15:34:00     75599             
          2       2398 2016-05-26   15:35:00     5822                   
          3       2399 2016-05-26   15:37:00     45225            
          4       2400 2016-05-26   15:38:00     5266  

信息

               NumeroM                  int64
                Date            datetime64[ns]
                Heure          timedelta64[ns]

我希望日期和小时列包含日期时间信息。即我想要

                      NumeroM Date      Heure          Reg 
 2016-05-26:15:32:00   2396   2016-05-26   15:32:00     2396           
 2016-05-26:15:34:00   2397   2016-05-26   15:34:00     75599 

我想要一个时间序列

【问题讨论】:

    标签: mysql python-2.7 pandas time-series


    【解决方案1】:

    我认为你可以使用加法,因为Date 列的dtypedatetime,而Heure 列是timedelta

    df.index = df.Date + df.Heure
    print (df)
                         NumeroM       Date    Heure    Reg
    2016-05-26 15:32:00     2396 2016-05-26 15:32:00   2396
    2016-05-26 15:34:00     2397 2016-05-26 15:34:00  75599
    2016-05-26 15:35:00     2398 2016-05-26 15:35:00   5822
    2016-05-26 15:37:00     2399 2016-05-26 15:37:00  45225
    2016-05-26 15:38:00     2400 2016-05-26 15:38:00   5266
    

    【讨论】:

    • 很高兴能为您提供帮助。美好的一天。顺便说一句,关于dtypes 的信息非常重要,尤其是timedelta
    • 但是新列有空格,OP示例显示没有空格。
    • @Marichyasana - 它只是格式化,因为文本 NumeroM 在此列名称下作为值更长。
    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    相关资源
    最近更新 更多