How do I convert date from alphabetical to numeric format? - python

I want to convert date from 'Sep 17, 2021' format to '17.09.2021'. I made a function, but I can't apply it to the series. What am I doing wrong?
def to_normal_date(bad_date):
datetime.strptime(bad_date, '%b %d, %Y')
return s.strftime('%Y-%m-%d')
df['normal_date'] = df['date'].apply(to_normal_date)
I receive a ValueError when I'm trying to apply it to series. But it works fine with this:
to_normal_date('Sep 16, 2021')

Use pd.to_datetime to convert the "date" column to datetime format. Specifying errors="coerce" will convert dates that are not in the correct format to NaN values instead of raising errors.
Convert to the required format using .strftime with the .dt accessor.
df["normal_date"] = pd.to_datetime(df["date"], format="%b %d, %Y", errors="coerce").dt.strftime("%d.%m.%Y")
>>> df
date normal_date
0 Sep 17, 2021 17.09.2021
1 Oct 31, 2021 31.10.2021
2 Nov 19, 2021 19.11.2021
3 Dec 25, 2021 25.12.2021

Try:
pd.to_datetime(df['date'], format='%b %d, %Y').dt.strftime('%Y-%m-%d')
It should work provided all df['date'] entries match the date pattern of 'Sep 17, 2021'.

Related

format a pandas dataframe column to datatime format [duplicate]

I have pandas column like following
January 2014
February 2014
I want to convert it to following format
201401
201402
I am doing following
df.date = pd.to_datetime(df.date,format= '%Y%B')
But,it gives me an error.
You shouldn't need the format string, it just works:
In [207]:
pd.to_datetime('January 2014')
Out[207]:
Timestamp('2014-01-01 00:00:00')
besides your format string is incorrect, it should be '%B %Y':
In [209]:
pd.to_datetime('January 2014', format='%B %Y')
Out[209]:
Timestamp('2014-01-01 00:00:00')

Convert seconds to date and time in python

I have a DataFrame with a column containing seconds and I would like to convert the column to date and time and save the file with a column containing the date and time .I Have a column like this in seconds
time
2384798300
1500353475
7006557825
1239779541
1237529231
I was able to do it but by only inserting the number of seconds that i want to convert with the following code:
datetime.fromtimestamp(1238479969).strftime("%A, %B %d, %Y %I:%M:%S")
output : Tuesday, March 31, 2009 06:12:49'
What i want to get is the conversion of the whole column,I tried this :
datetime.fromtimestamp(df['time']).strftime("%A, %B %d, %Y %I:%M:%S") but I can not get it, any help of how i can do it will be appreciated.
Use df.apply:
In [200]: from datetime import datetime
In [203]: df['time'] = df['time'].apply(lambda x: datetime.fromtimestamp(x).strftime("%A, %B %d, %Y %I:%M:%S"))
In [204]: df
Out[204]:
time
0 Friday, July 28, 2045 01:28:20
1 Tuesday, July 18, 2017 10:21:15
2 Wednesday, January 11, 2192 03:33:45
3 Wednesday, April 15, 2009 12:42:21
4 Friday, March 20, 2009 11:37:11

Converting a string month day, year to mm/dd/yyyy

I need to covert a string which contains date information (e.g., November 3, 2020) into date format (i.e., 11/03/2020).
I wrote
df['Date']=pd.to_datetime(df['Date']).map(lambda x: x.strftime('%m/%d/%y'))
where Date is
November 3, 2020
June 26, 2002
July 02, 2010
and many other dates, but I found the error ValueError: NaTType does not support strftime.
You can use pandas.Series.dt.strftime, which handles the NaT:
import pandas as pd
dates = ['November 3, 2020',
'June 26, 2002',
'July 02, 2010',
'NaT']
dates = pd.to_datetime(dates)
df = pd.DataFrame(dates, columns=['Date'])
df['Date'] = df['Date'].dt.strftime('%m/%d/%y')
Output:
Date
0 11/03/20
1 06/26/02
2 07/02/10
3 NaN

Convert data type series to datetime

I have a column that is a Series and I want to convert to a datetime format. This particular column of my data frame looks like below:
x
Aug 1, 2019 7:20:04 AM
Aug 1, 2019 7:20:14 AM
Aug 1, 2019 7:20:24 AM
Aug 1, 2019 7:20:34 AM
I've seem some answers here and I tried to adapt my code accordingly.
datetime.datetime.strptime(df["x"],'%b %d, %Y %H:%M:%S %a').strftime('%d/%m/%Y %H:%M:%S')
But I get the following error:
strptime() argument 1 must be str, not Series
For this reason, I tried to convert to string using the following:
df['x'] = df['x'].apply(str)
df['x'] = df['x'].to_string()
df['x'] = df['x'].astype(str)
But it does not work.
I am assuming you are using pandas. You can use pandas to_datetime() function instead of datetimes functions which can only convert a single value for a given call. Also for AM/PM you need %p instead of %a
df['x'] = pd.to_datetime(df['x'], format="%b %d, %Y %H:%M:%S %p")
Edit
Check to make sure your data is exactly how you posted it. I copy and pasted your data, and created a data frame and it works without an error.
df = pd.DataFrame({'x':['Aug 1, 2019 7:20:04 AM','Aug 1, 2019 7:20:14 AM','Aug 1, 2019 7:20:24 AM','Aug 1, 2019 7:20:34 AM']})
Output:
x
0 Aug 1, 2019 7:20:04 AM
1 Aug 1, 2019 7:20:14 AM
2 Aug 1, 2019 7:20:24 AM
3 Aug 1, 2019 7:20:34 AM
df['x'] = pd.to_datetime(df['x'],format='%b %d, %Y %H:%M:%S %p')
Output:
x
0 2019-08-01 07:20:04
1 2019-08-01 07:20:14
2 2019-08-01 07:20:24
3 2019-08-01 07:20:34

how to convert a string type to date format

My source data has a column including the date information but it is a string type.
Typical lines are like this:
04 13, 2013
07 1, 2012
I am trying to convert to a date format, so I used panda's to_datetime function:
df['ReviewDate_formated'] = pd.to_datetime(df['ReviewDate'],format='%mm%d, %yyyy')
But I got this error message:
ValueError: time data '04 13, 2013' does not match format '%mm%d, %yyyy' (match)
My questions are:
How do I convert to a date format?
I also want to extract to Month and Year and Day columns because I need to do some month over month comparison? But the problem here is the length of the string varies.
Your format string is incorrect, you want '%m %d, %Y', there is a reference that shows what the valid format identifiers are:
In [30]:
import io
import pandas as pd
t="""ReviewDate
04 13, 2013
07 1, 2012"""
df = pd.read_csv(io.StringIO(t), sep=';')
df
Out[30]:
ReviewDate
0 04 13, 2013
1 07 1, 2012
In [31]:
pd.to_datetime(df['ReviewDate'], format='%m %d, %Y')
Out[31]:
0 2013-04-13
1 2012-07-01
Name: ReviewDate, dtype: datetime64[ns]
To answer the second part, once the dtype is a datetime64 then you can call the vectorised dt accessor methods to get just the day, month, and year portions:
In [33]:
df['Date'] = pd.to_datetime(df['ReviewDate'], format='%m %d, %Y')
df['day'],df['month'],df['year'] = df['Date'].dt.day, df['Date'].dt.month, df['Date'].dt.year
df
Out[33]:
ReviewDate Date day month year
0 04 13, 2013 2013-04-13 13 4 2013
1 07 1, 2012 2012-07-01 1 7 2012

Categories

Resources