Home > Blockchain >  how to convert data which is in time delta into datetime.?
how to convert data which is in time delta into datetime.?

Time:12-22

|Event Date|startTime|
|----------|---------|
|2022-11-23|0 days 08:30:00|

when i was tring to get data a sql table to dataframe using variables from columns of other dataframe it came like this i want it only the time 08:30:00 what to do to get the required output

output I need is like this

|Event Date|startTime|
|----------|---------|
|2022-11-23|08:30:00|

i tried

sql['startTime']=pd.to_datetime(df1['startTime']).dt.time
it is showing this error
TypeError: <class 'datetime.time'> is not convertible to datetime

tried finding for it be didn't get anything useful solution but came across the opposite situation question still not useful info present in the question for my situation

CodePudding user response:

Add the timedelta to a datetime, then you have a time component. Ex:

import pandas as pd

df = pd.DataFrame({"Event Date": ["2022-11-23"],
                   "startTime": ["0 days 08:30:00"]})

# ensure correct datatypes; that might be not necessary in your case
df["Event Date"] = pd.to_datetime(df["Event Date"])
df["startTime"] = pd.to_timedelta(df["startTime"])

# create a datetime Series that includes the timedelta
df["startDateTime"] = df["Event Date"]   df["startTime"]

df["startDateTime"].dt.time
0    08:30:00
Name: startDateTime, dtype: object
  • Related