【问题标题】:How to index a datetime column from imported csv file - pandas如何从导入的 csv 文件中索引日期时间列 - pandas
【发布时间】:2016-11-08 21:31:22
【问题描述】:

我正在尝试合并和附加不同的时间序列,从 csv 文件中导入它们。我尝试了以下基本代码:

import pandas as pd
import numpy as np
import glob
import csv
import os

path = r'./A08_csv'     # use your path
#all_files = glob.glob(os.path.join(path, "A08_B1_T5.csv"))

df5 = pd.read_csv('./A08_csv/A08_B1_T5.csv', parse_dates={'Date Time'})
df6 = pd.read_csv('./A08_csv/A08_B1_T6.csv', parse_dates={'Date Time'})

print len(df5)
print len(df6)

df = pd.concat([df5],[df6], join='outer')
print len(df)

结果是:

12755 (df5)
24770 (df6)
12755 (df)

df 不应该与两个文件中最长的文件一样长(就 ['Date Time'] 列的值而言,它们有很多共同的行)??

我尝试根据日期时间索引数据,添加以下行:

#df5.set_index(pd.DatetimeIndex(df5['Date Time']))

但是我收到了错误:

KeyError: 'Date Time'

关于为什么会发生这种情况的任何线索?

【问题讨论】:

    标签: csv datetime pandas indexing concatenation


    【解决方案1】:

    我认为你需要:

    df5.set_index(['Date Time'], inplace=True)
    

    或者更好的在read_csv添加参数index_col:

    import pandas as pd
    import io
    
    temp=u"""Date Time,a
    2010-01-27 16:00:00,2.0
    2010-01-27 16:10:00,2.2
    2010-01-27 16:30:00,1.7"""
    
    df = pd.read_csv(io.StringIO(temp), index_col=['Date Time'], parse_dates=['Date Time'])
    print (df)
                           a
    Date Time               
    2010-01-27 16:00:00  2.0
    2010-01-27 16:10:00  2.2
    2010-01-27 16:30:00  1.7
    
    print (df.index)
    DatetimeIndex(['2010-01-27 16:00:00', '2010-01-27 16:10:00',
                   '2010-01-27 16:30:00'],
                  dtype='datetime64[ns]', name='Date Time', freq=None)
    

    另一种解决方案是按顺序将列添加到参数列 - 如果列Date Time 是第一个,则将0 添加到index_col 和parse_dates(python 计数来自0):

    import pandas as pd
    import io
    
    
    temp=u"""Date Time,a
    2010-01-27 16:00:00,2.0
    2010-01-27 16:10:00,2.2
    2010-01-27 16:30:00,1.7"""
    
    df = pd.read_csv(io.StringIO(temp), index_col=0, parse_dates=[0])
    print (df)
                           a
    Date Time               
    2010-01-27 16:00:00  2.0
    2010-01-27 16:10:00  2.2
    2010-01-27 16:30:00  1.7
    
    print (df.index)
    DatetimeIndex(['2010-01-27 16:00:00', '2010-01-27 16:10:00',
                   '2010-01-27 16:30:00'],
                  dtype='datetime64[ns]', name='Date Time', freq=None)
    

    【讨论】:

    • 谢谢。代码df5 = pd.read_csv('./A08_csv/A08_B1_T5.csv', index_col=['Date Time'], parse_dates=['Date Time']) 返回ValueError: 'Date Time' is not in list
    • 您还需要从列Date Time 设置索引吗?还是现在不需要了?
    • 如果需要,df5 = pd.read_csv('./A08_csv/A08_B1_T5.csv', parse_dates=['Date Time'])print df.dtypes之后是什么?
    • 是的,我需要它,否则 pd.concat (如上所述)将无法工作(另外我需要重新采样和计算均值等)。为什么这不起作用??
    • 你能回答我最后的评论吗? print df.dtypes 是什么?
    【解决方案2】:

    这是不正确的:

    pd.concat([df5],[df6], join='outer')
    

    concat 的第二个参数是axis。相反,你想要:

    pd.concat([df5, df6], join='outer')
    

    【讨论】:

      猜你喜欢
      • 2012-10-16
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 2023-02-18
      • 2017-10-18
      • 2017-08-13
      • 2016-07-28
      • 2015-02-21
      相关资源
      最近更新 更多