Home > OS >  How to combine/re-writes 'minute' in pandas.to_datetime?
How to combine/re-writes 'minute' in pandas.to_datetime?

Time:01-03

How to combine/re-writes 'minute' in pandas.to_datetime? My data frame looks like below:

df_2 = pd.DataFrame({
               'years' : df.year,
               'months': df.Month,
               'days'  : df.DayofMonth,
               'hours' : df.hour,
               'mins'  : df.min

                })
pd.to_datetime(df_2).head()

Result:

ValueError: extra keys have been passed to the datetime assemblage: [mins]

CodePudding user response:

The new name, to be recognized by pandas.to_datetime should be minute/minutes and you need column mins (not min which is a method) from df

'mins': df.min      # old
'minutes': df.mins  # new

Also that seems to be only a renaming, so you can use DataFrame.rename

df_2 = df.rename(columns={'DayofMonth': 'days', 'mins': 'minutes', 'sec': 'seconds'})
x = pd.to_datetime(df_2).head()

0   2015-08-21 19:34:00
1   2015-04-20 15:48:00
2   2015-09-02 14:22:00
3   2015-11-25 10:15:00
4   2015-10-07 18:28:00
dtype: datetime64[ns]

CodePudding user response:

Pandas Reference: https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html -->Examples

Examples

Assembling a datetime from multiple columns of a DataFrame. The keys can be common abbreviations like [‘year’, ‘month’, ‘day’, ‘minute’, ‘second’, ‘ms’, ‘us’, ‘ns’]) or plurals of the same

  • Related