I am trying to create 2 columns based of a column that contains numerical values.
Value
0
4
10
24
null
49
Expected Output:
Value Day Hour
0 Sunday 12:00am
4 Sunday 4:00am
10 Sunday 10:00am
24 Monday 12:00am
null No Day No Time
49 Tuesday 1:00am
Continued.....
Code I am trying out:
value = df.value.unique()
Sunday_Starting_Point = pd.to_datetime('Sunday 2015')
(Sunday_Starting_Point + pd.to_timedelta(Value, 'h')).dt.strftime('%A %I:%M%P')
Thanks for looking!
I think unique values are not necessary, you can use 2 times dt.strftime
for 2 columns with replace
with NaT
values:
Sunday_Starting_Point = pd.to_datetime('Sunday 2015')
x = pd.to_numeric(df.Value, errors='coerce')
s = Sunday_Starting_Point + pd.to_timedelta(x, unit='h')
df['Day'] = s.dt.strftime('%A').replace('NaT','No Day')
df['Hour'] = s.dt.strftime('%I:%M%p').replace('NaT','No Time')
print (df)
Value Day Hour
0 0.0 Sunday 12:00AM
1 4.0 Sunday 04:00AM
2 10.0 Sunday 10:00AM
3 24.0 Monday 12:00AM
4 NaN No Day No Time
5 49.0 Tuesday 01:00AM
Thanks, when I try I receive value error of ValueError: unit abbreviation w/o a number @jezrael
@Chris90 - It seems some non numeric values, can you check if working
s = Sunday_Starting_Point + pd.to_timedelta(df.Value, unit='h', errors='coerce')
insteads = Sunday_Starting_Point + pd.to_timedelta(df.Value, unit='h')
Thanks, it runs, but it gives same value of Sunday for Day and 12:00AM for Hour for every row
@Chris90 - It seems non numeric values, how working
x = pd.to_numeric(df.Value, errors='coerce') s = Sunday_Starting_Point + pd.to_timedelta(x, unit='h')
?Yeah this seems to populate and checks with what I am getting, thanks for helping to verify!