具有不同日期解析器的 Pandas read_csv

Kra*_*ars 5 python dataframe pandas panel-data

我有一个包含时间序列数据的 csv 文件,第一列是格式中的日期,%Y:%m:%d第二列是格式为“%H:%M:%S”的日内时间。我想将此 csv 文件导入多索引数据框或面板对象。

使用此代码,它已经可以工作:

    _file_data = pd.read_csv(_file,
                         sep=",",
                         header=0,
                         index_col=['Date', 'Time'],
                         thousands="'",
                         parse_dates=True,
                         skipinitialspace=True
                         )
Run Code Online (Sandbox Code Playgroud)

它以以下格式返回数据:

Date         Time                   Volume
2016-01-04   2018-04-25 09:01:29    53645
             2018-04-25 10:01:29    123
             2018-04-25 10:01:29    1345
             ....
2016-01-05   2018-04-25 10:01:29    123
             2018-04-25 12:01:29    213
             2018-04-25 10:01:29    123
Run Code Online (Sandbox Code Playgroud)

第一个问题:我想将第二个索引显示为纯时间对象而不是日期时间。为此,我必须在 read_csv 函数中声明两个不同的日期传递器,但我不知道如何。做到这一点的“最佳”方法是什么?

第二个问题:创建数据框后,我将其转换为面板对象。你会建议这样做吗?面板对象是这种数据结构的更好选择吗?面板对象的优点(缺点)是什么?

jez*_*ael 5

第一个问题

您可以创建多个converters解析器并在字典中定义解析器:

import pandas as pd

temp=u"""Date,Time,Volume
2016:01:04,09:00:00,53645
2016:01:04,09:20:00,0
2016:01:04,09:40:00,0
2016:01:04,10:00:00,1468
2016:01:05,10:00:00,246
2016:01:05,10:20:00,0
2016:01:05,10:40:00,0
2016:01:05,11:00:00,0
2016:01:05,11:20:00,0
2016:01:05,11:40:00,0
2016:01:05,12:00:00,213"""
Run Code Online (Sandbox Code Playgroud)
def converter1(x):
    #convert to datetime and then to times
    return pd.to_datetime(x).time()

def converter2(x):
    #define format of datetime
    return pd.to_datetime(x, format='%Y:%m:%d')

#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), 
                 index_col=['Date','Time'], 
                 thousands="'",
                 skipinitialspace=True,
                 converters={'Time': converter1, 'Date': converter2})

print (df)
                     Volume
Date       Time            
2016-01-04 09:00:00   53645
           09:20:00       0
           09:40:00       0
           10:00:00    1468
2016-01-05 10:00:00     246
           10:20:00       0
           10:40:00       0
           11:00:00       0
           11:20:00       0
           11:40:00       0
           12:00:00     213
Run Code Online (Sandbox Code Playgroud)

有时可以使用内置解析器,例如,如果日期格式为YY-MM-DD

import pandas as pd

temp=u"""Date,Time,Volume
2016-01-04,09:00:00,53645
2016-01-04,09:20:00,0
2016-01-04,09:40:00,0
2016-01-04,10:00:00,1468
2016-01-05,10:00:00,246
2016-01-05,10:20:00,0
2016-01-05,10:40:00,0
2016-01-05,11:00:00,0
2016-01-05,11:20:00,0
2016-01-05,11:40:00,0
2016-01-05,12:00:00,213"""
Run Code Online (Sandbox Code Playgroud)
def converter(x):
    #define format of datetime
    return pd.to_datetime(x).time()

#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), 
                 index_col=['Date','Time'], 
                 parse_dates=['Date'],
                 thousands="'",
                 skipinitialspace=True,
                 converters={'Time': converter})

print (df.index.get_level_values(0))
DatetimeIndex(['2016-01-04', '2016-01-04', '2016-01-04', '2016-01-04',
               '2016-01-05', '2016-01-05', '2016-01-05', '2016-01-05',
               '2016-01-05', '2016-01-05', '2016-01-05'],
              dtype='datetime64[ns]', name='Date', freq=None)
Run Code Online (Sandbox Code Playgroud)

最后一个可能的解决方案是在处理后通过- 转换datetime为时间:MultiIndexset_levels

df.index = df.index.set_levels(df.index.get_level_values(1).time, level=1)
print (df)
                     Volume
Date       Time            
2016-01-04 09:00:00   53645
           09:20:00       0
           09:40:00       0
           10:00:00    1468
2016-01-05 10:00:00     246
           10:00:00       0
           10:20:00       0
           10:40:00       0
           11:00:00       0
           11:20:00       0
           11:40:00     213
Run Code Online (Sandbox Code Playgroud)

第二个问题

pandas 0.20.+ 中的面板已弃用,并将在未来版本中删除。