0

I would like to convert a date of the format with yyyy=year, mm=month, dd=day, hh=hour, nn=minute in a unix timestamp. I tried:

df_out['unixtime'] = datetime(df_out['yyyymmddhhmm'].dt.year.to_numpy(),df_out['yyyymmddhhmm'].dt.month.to_numpy(),df_out['yyyymmddhhmm'].dt.day.to_numpy(),df_out['yyyymmddhhmm'].dt.hour.to_numpy(),df_out['yyyymmddhhmm'].dt.minute.to_numpy()).timestamp()

but I got the error message:

TypeError: only size-1 arrays can be converted to Python scalars

What am I doing wrong?

Any help is highly appreciated!

Regards, Alexander

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
akann
  • 37
  • 4

2 Answers2

1

The officially recommended way is to subtract the epoch and then to floor-divide by the “unit” (1 second):

df = pd.DataFrame({'yyyymmddhhmm': pd.to_datetime(['20201108121314', '20201109121314'])})

df['unixtime'] = (df.yyyymmddhhmm - pd.Timestamp('1970-01-01')) // pd.Timedelta('1s')

Result:

         yyyymmddhhmm    unixtime
0 2020-11-08 12:13:14  1604837594
1 2020-11-09 12:13:14  1604923994
Stef
  • 28,728
  • 2
  • 24
  • 52
0

You can create a single column for the date using the pandas library

 df_out['date_format'] = pd.to_datetime(df_out['date_time_column'], format='%Y%m%d%H%M')

Then you can create new columns which will consist of year, month, date, hour info by

pd.DatetimeIndex(df_out['date_format']).year 
pd.DatetimeIndex(df_out['date_format']).month 
pd.DatetimeIndex(df_out['date_format']).day 
pd.DatetimeIndex(df_out['date_format']).hour
vnikonov_63
  • 191
  • 12