代码之家  ›  专栏  ›  技术社区  ›  Pythonista anonymous

Python:如何创建一个固定日期+另一列中的天的列

  •  0
  • Pythonista anonymous  · 技术社区  · 6 年前

    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:('must be str,not int','occurred at index 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')

    我做错什么了?

    2 回复  |  直到 5 年前
        1
  •  3
  •   jezrael    6 年前

    使用 to_timedelta 用于将值转换为日时间增量或使用 origin 使用参数指定开始日期 unit 在里面 to_datetime

    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  
    
        2
  •  2
  •   cs95 abhishek58g    6 年前

    你可以用 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\ U时间增量 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