Split Datetime Column into a Date and Time Python

Split datetime into date and time

Always use a date library, to avoid the many errors that can happen when you try to do it yourself. In this case, it looks like November the 31st, which is not a valid day.

https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime

Of course, you'll have to help it with the format. We're just going to ignore the T, by making it a literal in the format string. See What exactly does the T and Z mean in timestamp?

import datetime
format_string = "%Y-%m-%dT%H:%M:%S.%f"
date_string = "2015-11-31T13:45:00.000"
datetime.datetime.strptime(date_string, format_string)

builtins.ValueError: day is out of range for month

Once you have the datetime, it will be relatively easy to print out whatever components you want with strftime. Of course, you could just split on 'T' if that literal is always there, but then you wouldn't really have reliable time objects. It wouldn't tell you that Nov 31 is not a valid day.

Pandas: Splitting datetime into weekday, month, hour columns

Create a custom function:

# Use {i:02} to get a number on two digits
cols = [f'weeday_{i}' for i in range(1, 8)] \
+ [f'hour_{i}' for i in range(1, 25)] \
+ [f'month_{i}' for i in range(1, 13)]

def get_dummy(dt):
l = [0] * (7+24+12)
l[dt.weekday()] = 1
l[dt.hour + 6] = 1
l[dt.month + 30] = 1
return pd.Series(dict(zip(cols, l)))

df = df.join(df['datetime'].apply(get_dummy))

Output:

>>> df.iloc[0]
datetime 2012-04-01 07:00:00
weeday_1 0
weeday_2 0
weeday_3 0
weeday_4 0
weeday_5 0
weeday_6 0
weeday_7 1 # <- Sunday
hour_1 0
hour_2 0
hour_3 0
hour_4 0
hour_5 0
hour_6 0
hour_7 1 # <- 07:00
hour_8 0
hour_9 0
hour_10 0
hour_11 0
hour_12 0
hour_13 0
hour_14 0
hour_15 0
hour_16 0
hour_17 0
hour_18 0
hour_19 0
hour_20 0
hour_21 0
hour_22 0
hour_23 0
hour_24 0
month_1 0
month_2 0
month_3 0
month_4 1 # <- April
month_5 0
month_6 0
month_7 0
month_8 0
month_9 0
month_10 0
month_11 0
month_12 0
Name: 0, dtype: object


Related Topics



Leave a reply



Submit