简体   繁体   中英

Converting a string that has day of the year to datetime

I have a string column that looks like below:

2018-24 7:10:0      
2018-8 12:1:20      
2018-44 13:55:19    

The 24,8,44 that you see are the day of the year and not the date.
How can I convert this to datetime column in the below format ?

2018-01-24 07:10:00
2018-01-08 12:01:20
2018-02-13 13:55:19

I am unable to find anything related to converting day of the year ?

You need format string '%Y-%j %H:%M:%S'

In[53]:
import datetime as dt
dt.datetime.strptime('2018-44 13:55:19', '%Y-%j %H:%M:%S')

Out[53]: datetime.datetime(2018, 2, 13, 13, 55, 19)

%j is day of year

For pandas :

In[59]:
import pandas as pd
import io
t="""2018-24 7:10:0
2018-8 12:1:20
2018-44 13:55:19"""
df = pd.read_csv(io.StringIO(t), header=None, names=['datetime'])
df

Out[59]: 
           datetime
0    2018-24 7:10:0
1    2018-8 12:1:20
2  2018-44 13:55:19

Use pd.to_datetime and pass format param:

In[60]:
df['new_datetime'] = pd.to_datetime(df['datetime'], format='%Y-%j %H:%M:%S')
df

Out[60]: 
           datetime        new_datetime
0    2018-24 7:10:0 2018-01-24 07:10:00
1    2018-8 12:1:20 2018-01-08 12:01:20
2  2018-44 13:55:19 2018-02-13 13:55:19

You can use dateutil.relativedelta for sum the day from the first day of years.

example:

from datetime import datetime
from dateutil.relativedelta import relativedelta

datetime.now()+ relativedelta(days=5)

The documentation at strftime.org identifies the %j format specifier as handling day of the year. I don't know whether it's available on all platforms, but my Mac certainly has it.

Use time.strptime to convert from string to datetime. The output below has a newline inserted for reading convenience:

>>> time.strptime('2018-24 7:10:0', '%Y-%j %H:%M:%S')
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=24, tm_hour=7,
       tm_min=10, tm_sec=0, tm_wday=2, tm_yday=24, tm_isdst=-1)

The time.strftime formats datetimes, so you can get what you need by applying it to the output of strptime :

>>> time.strftime('%Y-%m-%d %H:%M:%S',
...               time.strptime('2018-24 7:10:0', '%Y-%j %H:%M:%S'))
'2018-01-24 07:10:00'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM