【问题标题】:Python pandas: how to create a column which is a fixed date + the # days in another columnPython pandas:如何创建一个固定日期的列+另一列中的#天
【发布时间】:2019-07-26 18:39:06
【问题描述】:

我需要向数据框添加一列,以便第 0 行是 2019 年 2 月 15 日。第 1 行是第 16 行,等等。我尝试过使用索引:

import numpy as np
import pandas as pd
df=pd.DataFrame()
df['a']=np.arange(10,20)
df['date from index']=df.apply( lambda x: pd.to_datetime('15-2-2019') + pd.DateOffset(days=x.index), axis=1 )

但我明白了:

TypeError: ('必须是 str,而不是 int','发生在索引 0')

我承认我不明白。 我尝试创建一个显式列来代替索引:

df=pd.DataFrame()
df['a']=np.arange(10,20)
df['counter']=np.arange(0,df.shape[0])
df['date from counter']=df.apply( lambda x: pd.to_datetime('15-2-2019') + pd.DateOffset(days=x['counter']), axis=1 )

但这给了我:

TypeError: ('timedelta days 组件不支持的类型: numpy.int32', '发生在索引 0')

我做错了什么?

【问题讨论】:

  • 谢谢你们!只是为了我的理解,我在我的例子中做错了什么?
  • 编辑了我的答案

标签: python pandas date


【解决方案1】:

您可以使用 pd.to_timedelta 将其矢量化:

# pd.to_timedelta(df.index, unit='d') + pd.to_datetime('15-2-2019') # whichever
pd.to_timedelta(df.a, unit='d') + pd.to_datetime('15-2-2019')

0   2019-02-25
1   2019-02-26
2   2019-02-27
3   2019-02-28
4   2019-03-01
5   2019-03-02
6   2019-03-03
7   2019-03-04
8   2019-03-05
9   2019-03-06
Name: a, dtype: datetime64[ns]

df['date_from_counter'] = (
    pd.to_timedelta(df.a, unit='d') + pd.to_datetime('15-2-2019'))
df

    a  counter date_from_counter
0  10        0        2019-02-25
1  11        1        2019-02-26
2  12        2        2019-02-27
3  13        3        2019-02-28
4  14        4        2019-03-01
5  15        5        2019-03-02
6  16        6        2019-03-03
7  17        7        2019-03-04
8  18        8        2019-03-05
9  19        9        2019-03-06

正如预期的那样,您可以在任何具有正确单位的整数列上调用 pd.to_timedelta,然后使用生成的 Timedelta 列进行日期时间算术。


要使您的代码正常工作,您似乎需要传递int,而不是np.int(不知道为什么)。这行得通。

dt = pd.to_datetime('15-2-2019')
df['date from counter'] = df.apply(
    lambda x: dt + pd.DateOffset(days=x['counter'].item()), axis=1)
df

    a  counter date from counter
0  10        0        2019-02-15
1  11        1        2019-02-16
2  12        2        2019-02-17
3  13        3        2019-02-18
4  14        4        2019-02-19
5  15        5        2019-02-20
6  16        6        2019-02-21
7  17        7        2019-02-22
8  18        8        2019-02-23
9  19        9        2019-02-24

【讨论】:

    【解决方案2】:

    使用to_timedelta 将值转换为日时间增量或使用 参数originto_datetime 中使用参数unit 指定开始日期:

    df['date from index']= pd.to_datetime('15-2-2019') + pd.to_timedelta(df.index, 'd')
    df['date from counter']= pd.to_datetime('15-2-2019') + pd.to_timedelta(df['counter'], 'd')
    
    df['date from index1']= pd.to_datetime(df.index, origin='15-02-2019', unit='d')
    df['date from counter1']= pd.to_datetime(df['counter'], origin='15-02-2019', unit='d')
    print(df.head())
        a  counter date from index date from counter date from index1  \
    0  10        0      2019-02-15        2019-02-15       2019-02-15   
    1  11        1      2019-02-16        2019-02-16       2019-02-16   
    2  12        2      2019-02-17        2019-02-17       2019-02-17   
    3  13        3      2019-02-18        2019-02-18       2019-02-18   
    4  14        4      2019-02-19        2019-02-19       2019-02-19   
    
      date from counter1  
    0         2019-02-15  
    1         2019-02-16  
    2         2019-02-17  
    3         2019-02-18  
    4         2019-02-19  
    

    【讨论】:

      猜你喜欢
      • 2023-02-07
      • 2019-12-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 2023-01-28
      • 2017-03-07
      相关资源
      最近更新 更多