【问题标题】:Pandas read_csv parse_dates from 2 columns来自 2 列的 Pandas read_csv parse_dates
【发布时间】:2018-10-29 23:34:39
【问题描述】:

我在这里没有找到非常适合我的情况的帖子。我有一个 csv 文件,其中第一列是年(2002 年),第二列是月份(一月),第三列是 MonthCode(1 表示一月等)。我想导入 Pandas 数据框以创建完整日期索引。下面的代码给出了一个错误,但应该告诉你我正在尝试做什么。

错误是: ValueError: 时间数据 '2002' 与格式 '%Y%b%d' 不匹配

注意:我的数据中没有月份中的某一天,因此我必须使用第一天或最后一天,除非有办法仅对没有日期的年份和月份进行索引。

数据如下:

Year    Month   Month Code  District Code   District
2002    January 1   1   Albany
2002    January 1   2   Allegany
2002    January 1   3   Broome
2002    January 1   4   Cattaraugus
2002    January 1   5   Cayuga

不起作用的代码:

file = 'C:/.../snap.csv'

parser = lambda date: pd.datetime.strptime(date, '%Y%b%d')


# create dataframe from csv file
snapdf = pd.read_csv(file, parse_dates = [0,1], date_parser = parser)
# NOTE: I also tried parse_dates = [0,2] but same error

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    我更改了数据,以便更清楚地将日期解析到数据帧中的方式

    Year,Month,Month Code,District Code,District
    2002,January,1,1,Albany
    2004,February,1,2,Allegany
    2005,December,1,3,Broome
    2007,August,1,4,Cattaraugus
    2001,March,1,5,Cayuga
    

    在第 1-3 列中使用 parse_dates 参数:

    >>>> with open('snap.csv') as f:
           df = pd.read_csv(f, parse_dates={'Date': [0,1,2]}, index_col='Date')
    
    >>>> df
                District Code     District
    Date                                  
    2002-01-01              1       Albany
    2004-02-01              2     Allegany
    2005-12-01              3       Broome
    2007-08-01              4  Cattaraugus
    2001-03-01              5       Cayuga
    
    >>>> df.District
    Date
    2002-01-01         Albany
    2004-02-01       Allegany
    2005-12-01         Broome
    2007-08-01    Cattaraugus
    2001-03-01         Cayuga
    Name: District, dtype: object
    

    【讨论】:

    • 谢谢,但它不起作用。如果您使用第 1 列 - iow,第 1 列中的 1 月作为第 2 列中的第 1 个月,则第 2 列无关紧要。当您到达 2 月时,它看起来与 January:70 2002-01-01 February 2 13 Dutchess 71 2002 相同-01-01 2 月 2 日 14 日伊利 72 2002-01-01 2 月 2 日 15 日埃塞克斯 73 2002-01-01 2 月 2 日 16 日富兰克林
    • 您的预期结果是什么?我的示例使用 csv 中的 Month code 列作为日期替换,因此所有日期都是本月的 1 日。您的评论包括默认编号索引,这表明您没有在我的代码中包含index_col='Date' 标志(这应该导致DatetimeIndex 数据框-> DatetimeIndex(['2002-01-01', '2004-02-01', '2005-12-01', '2007-08-01', '2001-03-01'], dtype='datetime64[ns]', name='Date', freq=None)
    • 它有效,除了一个问题。月份代码是月份的数字,所以二月是02,三月是03,等等。所以你不能用它作为日。这样做会在 2 月导致以下情况 - 2002-02-02 14 Erie 36161 2002-02-02 15 Essex 1151 2002-02-02 16 Franklin 1647 注意 2 月的所有日期都是第二个。在三月,所有日期都是第三天,等等。如果可以的话,我基本上需要将 Day 01 硬编码到 read_csv 代码中。或者事后做。谢谢
    【解决方案2】:

    我终于把它运行起来了,它实际上非常简单。

    snapdf["DateIndex"] = pd.to_datetime(snapdf['Year'].astype(str), format='%Y')
    

    这会从数据框的 Year 列中获取值(存储为 Int),并将其转换为新列 DateIndex 中的日期字符串。由于没有月份或日期数据,它会自动插入 01/01 作为月份和日期。

    因此,Year 列中的 2017 变为 01/10/2017

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 2021-04-18
      • 2015-06-28
      • 2019-07-11
      • 2017-04-21
      • 1970-01-01
      相关资源
      最近更新 更多