How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas?

How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas?

Problem Description:

I have data of the form,

YearDayHourMinuteField magnitude average, nTSpeed, km/sAE-index, nTSYM/H, nTASY/H, nTPCN-index
02017250008.27390.611742202.00
12017250013.14405.312142191.64
22017250023.86434.212442211.72
32017250034.17473.214142191.92
42017250043.43497.010741191.70

How can I create a datetime object in Pandas using the Year, Day, Hour, Minute columns? What’s the best approach?

I tried using

pd.to_datetime(dict(year=df.Year,day=df.Day,month=None))

which failed!

Also tried,

df["DateTime"] = pd.to_datetime(df[["Year","Day","Hour","Minute"]]).dt.strftime('%d/%y %H:%M')

Solution – 1

You can merge as a single string and use to_datetime with a custom format ('%Y-%j-%H-%M' for Year-DayOfYear-Hours-Minutes):

pd.to_datetime(df[['Year', 'Day', 'Hour', 'Minute']]
               .astype(str).agg('-'.join, axis=1), format='%Y-%j-%H-%M')

Output:

0   2017-09-07 00:00:00
1   2017-09-07 00:01:00
2   2017-09-07 00:02:00
3   2017-09-07 00:03:00
4   2017-09-07 00:04:00
dtype: datetime64[ns]

Solution – 2

I think your Day column is JULIAN date, i.e. when 1st Jan is 1, then julian 250 is 9th July (in non leap year). If that is so, you can create additional column YJ to concatenate Year and Day and then apply datetime creation.

import pandas as pd
data = [{'Year':2017, 'Day':250, 'Hour':0, 'Minute':0, 'Data': 8.27},
        {'Year':2017, 'Day':250, 'Hour':1, 'Minute':0, 'Data': 3.14}]
df= pd.DataFrame(data)
df['YJ'] = (df['Year'].apply(str)+df['Day'].apply(str)) #convert to string
df['DateTime'] = (df['YJ'].apply(lambda x: dt.datetime.strptime(x,'%Y%j'))) #new column

#output: df.head()
YearDayHourMinuteDataYJDateTime
02017250008.2720172502017-09-07
12017250103.1420172502017-09-07
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject