How to convert string/float to time in python/pandas? - python

I have a dataset which stores durations like 3 hours and 7 minutes in the format of, 3.11 as a string.
I want to convert the column containing these values into datetime in a way that I get: 03:07.
When I do:
df["ConnectedDuration"] = pd.to_datetime(df['ConnectedDuration'])
I get: 1970-01-01 00:00:00.000000003 which is obviousely not what I want.
When I do:
df["ConnectedDuration"] = pd.to_datetime(df['ConnectedDuration'], format='%H:%M')
I get the following error: ValueError: time data '3' does not match format '%H:%M' (match)
Any help is highly appreciated

You want to convert this values to timedelta instead of datetime. Thus you should use the pd.to_timedelta method, like:
pd.to_timedelta(df["ConnectedDuration"].astype('float'), unit='h')

Related

How do I convert object type data to datetime data in this case?

So I have data given in the format of:
1/1/2022 0:32
I looked up the type that was given with dataframe.dytpe and found out that this was an object type. Now for my further analysis I think it would be best to get this converted to a datetime data type.
In order to do so, I tried using
dataframe["time"] = pd.to_datetime(["time"], format = "%d%m%y")
, though that just leaves me with NaT showing up in the rows where I want my time to appear. What is the correct way to convert my given data?
Would it be better to split time and date or is there a way to convert the whole?
You can do like this:
df['time'] = pd.to_datetime(df['time']).dt.normalize()
or
df["time"]=df["time"].astype('datetime64')
it will convert object type to datetime64[ns]
I think datetime64[ns] is always in YYYY-MM-DD format, if you want to change your format you can use:
df['time'] = df['time'].dt.strftime('%m/%d/%Y')
You can change the '%m/%d/%Y'according to your desired format. But, The datatype will again change to object by using strftime.

Aligning datetime formats for comparrison

I'm having trouble align two different dates. I have an excel import which I turn into a DateTime in pandas and I would like to compare this DateTime with the current DateTime. The troubles are in the formatting of the imported DateTime.
Excel format of the date:
2020-07-06 16:06:00 (which is yyyy-dd-mm hh:mm:ss)
When I add the DateTime to my DataFrame it creates the datatype Object. After I convert it with pd.to_datetime it creates the format yyyy-mm-dd hh:mm:ss. It seems that the month and the day are getting mixed up.
Example code:
df = pd.read_excel('my path')
df['Arrival'] = pd.to_datetime(df['Arrival'], format='%Y-%d-%m %H:%M:%S')
print(df.dtypes)
Expected result:
2020-06-07 16:06:00
Actual result:
2020-07-06 16:06:00
How do I resolve this?
Gr,
Sempah
An ISO-8601 date/time is always yyyy-MM-dd, not yyyy-dd-MM. You've got the month and date positions switched around.
While localized date/time strings are inconsistent about the order of month and date, this particular format where the year comes first always starts with the biggest units (years) and decreases in unit size going right (month, date, hour, etc.)
It's solved. I think that I misunderstood the results. It already was working without me knowledge. Thanks for the help anyway.

In Jupyter/Python/Pandas dataframe, Modifying a Date Time field from String to Date Time

I have created a dataframe through an API extract one of the fields of which is Datetime in the format YYYY-MM-DD HH:MM:SS. I am trying to convert this to a datetime format using the following command:
df_weather['DATETIME'] = pd.to_datetime( df_weather.DATETIME )
But I am getting errors, the last line of which is:
ValueError: ('Unknown string format:', '2018-01-01 0.00.00')
Is the problem that the Hours field is showing only 1 digit instead of a zero-padded value for values less than 10? If yes, how to correct that?
If no, what could be the problem here and how to resolve?
This is error is not because of hour with only one digit. It is because of your time format is not correct.
You can use this:
df_weather['DATETIME'] = pd.to_datetime(df_weather.DATETIME,format="%Y-%m-%d %H.%M.%S")
For time format, you can check this site: https://strftime.org/

How to convert date to datetime?

I have this type of date '20181115 0756' and also in a diffent dataframe in this format '2018-11-15'. I would like to know if there is any way to convert it to datetime without the hours and minutes
date['DATE']= pd.to_datetime(date.DATE)
this converts it to 218-11-15 00:00:00 and I'd like to avoid that
What I trying to do is to calcuate the time difference between the dates in the two dataframes that I have
Thank you in advance
You can use the following code
date['DATE'] = pd.to_datetime(date['DATE'], errors='coerce').dt.date

Converting a series of DateTime values to the proper format

Currently, I have a series of Datetime Values that display as so
0 Datetime
1 20041001
2 20041002
3 20041003
4 20041004
they are within a series named
d['Datetime']
They were originally something like
20041001ABCDEF
But I split the end off just to leave them with the remaining numbers. How do I go about putting them into the following format?
2004-10-01
You can do the following,
df['Datetime'] = pd.to_datetime(df['Datetime'], format='%Y%m%d'))

Categories

Resources