Convert to DateTime datatype in Pandas - pandas

How do I convert a pandas column to datetime format?
I have tried this and it did not work.
odu1['DateTime'] = pd.to_datetime(odu1['DateTime'].str.strip(), format='%m/%d/%Y %I:%M:%S %p')
Here is one example entry of how values in my column look like 06/01/2022 12:00:06 AM

Related

Pandas - Converting datetime field to a specified format

I am trying to get a date time field in Pandas in the below format
df['date'] = pd.to_datetime(df['date'])
The above code returns date time column in the below format
2021-11-27 03:30:00
I would like to get an output of 27/11/2021 (format is dd/mm/yyyy) and the data type of the column needs to be datetime and not object.
If your column is a string, you will need to first use pd.to_datetime,
df['Date'] = pd.to_datetime(df['Date'])
Then, use .dt datetime accessor with strftime:
df = pd.DataFrame({'Date':pd.date_range('2017-01-01', periods = 60, freq='D')})
df.Date.dt.strftime('%Y%m%d').astype(int)
Or use lambda function:
df.Date.apply(lambda x: x.strftime('%Y%m%d')).astype(int)

Convert TZ datetime to timestamp

I have column of dates in the format below in a pandas dataframe.
What is the most effective way to convert
2021-11-06T21:54:35.825Z
to
2021-11-6 21:54:35
pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S') only returns 2021-11-06 without the timestamp
You can use .dt accessor on Pandas Series followed by by .strftime property dt.strftime, to format datetime into desired string representation.
import pandas as pd
import datetime
df = pd.DataFrame({'date': ["2021-11-06T21:54:35.825Z"]})
fmt = '%Y-%m-%d %H:%M:%S'
pd.to_datetime(df['date']).dt.strftime(fmt)
returns
0 2021-11-06 21:54:35
Name: date, dtype: object
Or if you don't want to have zero padding before the day, you can use: fmt="%Y-%m-%-d %H:%M:%S" (notice the hyphen between % and d). This results in: 2021-11-6 21:54:35

pandas string to date type conversion in proper format

I am getting date data in string format in pandas like 10-Oct,11-Oct but i want to make it date data type like this format 2019-10-10,2019-10-11
is there any easy way available in pandas?
Use to_datetime with added year and parameter format:
df = pd.DataFrame({'date':['10-Oct', '11-Oct']})
df['date'] = pd.to_datetime(df['date'] + '-2019', format='%d-%b-%Y')
print (df)
date
0 2019-10-10
1 2019-10-11

trying to format pandas.to_datetime

I'm trying to get today's date in a few different formats and I keep getting errors:
pd.to_datetime('Today',format='%m/%d/%Y') + MonthEnd(-1)
ValueError: time data 'Today' does not match format '%m/%d/%Y' (match)
What is the correct syntax to get todays date in yyyy-mm-dd and yyyymm formats?
For YYYY-MM-DD format, you can do this:
import datetime as dt
print(dt.datetime.today().date())
2017-05-23
For YYYY-MM format, you can do this:
print(dt.datetime.today().date().strftime('%Y-%m'))
2017-05
If you need to do this on just a few columns you can use:
import pandas as pd
dataframe_name['Date_Column_name'].apply(pd.tslib.normalize_date)
This method doesn't use any other module except pandas. If you need a "custom" date format you can always do:
from datetime import datetime as dt
dataframe_name['Date_Column_name'].dt.strftime('%d/%m/%Y')
Here is a list of strftime options.

pandas dataframe converting complex date-format to date dd/mm/yyyy hh:mm

I'm using python and pandas to transform csv's,
I imported the csv as a dataframe.
the dataframe has a string-column containing dates in format "January 1, 2016, Hour 0"
I'd like to convert that string into a date or even string in format dd/mm/yyyy hh:mm
note that in this case the minutes will always be 0.
thanks !
I've done it using datetime and strptime as following
df['field'].apply(lambda x : datetime.strptime(x, '%B %d, %Y, Hour %H').strftime('%d/%m/%Y %H:00'))
or generally:
df['field'].apply(lambda x : datetime.strptime(x, old_formatted_date).strftime(new_formatted_date))
for more on datetime default lib check:
datetime
and datetime format codes:
datetime format codes