user308827
user308827

Reputation: 22041

Converting year, month and day to datetime

I have the foll. datafrme:

     YEAR   MO   DY  name_col 
0  2016.0  1.0  5.0     0.00 
1  2016.0  1.0  6.0     0.00 
2  2016.0  1.0  7.0     0.41 
3  2016.0  1.0  8.0     0.53 
4  2016.0  1.0  9.0     2.12 

How do I convert the YEAR, MO and DY columns representing year, month and day respectively to datetime?

     YEAR   MO   DY  name_col   datetime
0  2016.0  1.0  5.0     0.00 2016-01-05
1  2016.0  1.0  6.0     0.00 2016-01-06
2  2016.0  1.0  7.0     0.41 2016-01-07
3  2016.0  1.0  8.0     0.53 2016-01-08
4  2016.0  1.0  9.0     2.12 2016-01-09

I tried this:

pd.to_datetime(df_met['YEAR'].astype(int), format='%Y') + pd.to_timedelta(df_met['DY'] - 1, unit='d')

Upvotes: 0

Views: 654

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

pd.to_datetime() can assemble datetime from multiple columns if columns have "correct" names:

In [106]: df
Out[106]:
     YEAR   MO   DY  name_col
0  2016.0  1.0  5.0      0.00
1  2016.0  1.0  6.0      0.00
2  2016.0  1.0  7.0      0.41
3  2016.0  1.0  8.0      0.53
4  2016.0  1.0  9.0      2.12

In [107]: df['datetime'] = pd.to_datetime(df.rename(columns={'MO':'MONTH', 'DY':'DAY'}).drop('name_col', 1))

In [108]: df
Out[108]:
     YEAR   MO   DY  name_col   datetime
0  2016.0  1.0  5.0      0.00 2016-01-05
1  2016.0  1.0  6.0      0.00 2016-01-06
2  2016.0  1.0  7.0      0.41 2016-01-07
3  2016.0  1.0  8.0      0.53 2016-01-08
4  2016.0  1.0  9.0      2.12 2016-01-09

from docs:

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

Upvotes: 2

Related Questions