0
           local_time
5398    2019-02-14 14:35:42+01:00
5865    2021-09-22 04:28:53+02:00
6188    2018-05-04 09:34:53+02:00
6513    2019-11-09 15:54:51+01:00
6647    2019-09-18 09:25:43+02:00

df_with_local_time['local_time'].loc[6647] returns

 datetime.datetime(2019, 9, 18, 9, 25, 43, tzinfo=<DstTzInfo 'Europe/Oslo' CEST+2:00:00 DST>)

Based on the column, I would like to generate multiple date-related columns:

def datelike_variables(i):
    year = i.year
    month = i.month
    #dayofweek = i.dayofweek
    day = i.day
    hour = i.hour
    return year, month, day, hour

df_with_local_time[['year','month','day','hour']]=df_with_local_time['local_time'].apply(datelike_variables,axis=1,result_type="expand")

returns TypeError: datelike_variables() got an unexpected keyword argument 'result_type'

Expected result:

           local_time               year      month      day       hour
5398    2019-02-14 14:35:42+01:00   2019        02        14        14
5865    2021-09-22 04:28:53+02:00   2021        09        22        04
6188    2018-05-04 09:34:53+02:00   2018        05        04        09
6513    2019-11-09 15:54:51+01:00   2019        11        09        15
6647    2019-09-18 09:25:43+02:00   2019        09        18        09
Luc
  • 737
  • 1
  • 9
  • 22
  • Might want to check this https://stackoverflow.com/questions/39050539/how-to-add-multiple-columns-to-pandas-dataframe-in-one-assignment – Franco Piccolo Dec 15 '21 at 10:36

1 Answers1

0

Error is because use Series.apply, there is no parameter result_type:

def datelike_variables(i):
    year = i.year
    month = i.month
    #dayofweek = i.dayofweek
    day = i.day
    hour = i.hour
    return pd.Series([year, month, day, hour])

df_with_local_time[['year','month','day','hour']]=df_with_local_time['local_time'].apply(datelike_variables)
print (df_with_local_time)
                     local_time  year  month  day  hour
5398  2019-02-14 14:35:42+01:00  2019      2   14    14
5865  2021-09-22 04:28:53+02:00  2021      9   22     4
6188  2018-05-04 09:34:53+02:00  2018      5    4     9
6513  2019-11-09 15:54:51+01:00  2019     11    9    15
6647  2019-09-18 09:25:43+02:00  2019      9   18     9

Your solution is possible by lambda function in DataFrame.apply:

def datelike_variables(i):
    year = i.year
    month = i.month
    #dayofweek = i.dayofweek
    day = i.day
    hour = i.hour
    return year, month, day, hour

df_with_local_time[['year','month','day','hour']]=df_with_local_time.apply(lambda x: datelike_variables(x['local_time']), axis=1,result_type="expand")
print (df_with_local_time)
                     local_time  year  month  day  hour
5398  2019-02-14 14:35:42+01:00  2019      2   14    14
5865  2021-09-22 04:28:53+02:00  2021      9   22     4
6188  2018-05-04 09:34:53+02:00  2018      5    4     9
6513  2019-11-09 15:54:51+01:00  2019     11    9    15
6647  2019-09-18 09:25:43+02:00  2019      9   18     9
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • TypeError: datelike_variables() got an unexpected keyword argument 'result_type' – Luc Dec 15 '21 at 10:42