Issue
df = {'ArrivalPeriod': [2018-10-30 06:49:57, 2019-11-30 07:42:53, 2018-11-28 02:21:57, 2018-10-30 06:49:57],
'time': [12, 8, 10, 10],
'weekday': [4, 6, 5, 1]}
df = pd.DataFrame(df)
df
df['Arrival_Time'] = pd.DatetimeIndex(df['ArrivalDate']).time
df['Arrival_Date'] = pd.DatetimeIndex(df['ArrivalDate']).Date
df["Arrival_Time"] = df["Arrival_Time"].str.split(':').apply(lambda x: int(x[0]) * 60 + int(x[1]))
My expected output is to convert the time seconds.
Solution
Try using .dt
:
df = pd.DataFrame({'ArrivalPeriod': ["2018-10-30 06:49:57", "2019-11-30 07:42:53", "2018-11-28 02:21:57", "2018-10-30 06:49:57"],
'time': [12, 8, 10, 10],
'weekday': [4, 6, 5, 1]})
df["ArrivalPeriod"] = pd.to_datetime(df["ArrivalPeriod"])
df["Arrival_Time"] = df["ArrivalPeriod"].dt.time
df["Arrival_Date"] = df["ArrivalPeriod"].dt.date
>>> df
ArrivalPeriod time weekday Arrival_Time Arrival_Date
0 2018-10-30 06:49:57 12 4 06:49:57 2018-10-30
1 2019-11-30 07:42:53 8 6 07:42:53 2019-11-30
2 2018-11-28 02:21:57 10 5 02:21:57 2018-11-28
3 2018-10-30 06:49:57 10 1 06:49:57 2018-10-30
If you want to get the total seconds from the time, you can then do:
>>> pd.to_timedelta(df["Arrival_Time"].astype(str)).dt.total_seconds()
0 24597.0
1 27773.0
2 8517.0
3 24597.0
Name: Arrival_Time, dtype: float64
Answered By - not_speshal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.