How to convert different date formats in pandas? - python-3.x

I have 2 columns with different date formats. In every row string dates are formatted differently.
I want to convert the columns to Date type. However, I am wondering if there is any built in method that will do the parsing for me:
What I tried
from datetime import datetime
newFrame = newDF.assign(Effective_Date=newDF['Effective_Date'].apply(lambda element: datetime.strptime(element,'%b %d %Y %H %M %S')), Paid_Off_Time=newDF['Paid_Off_Time'].apply(lambda element: datetime.strptime(element,'%b %d %Y %H %M %S')))
error when I run code above
line 359, in _strptime
(data_string, format))
ValueError: time data '09/08/2016' does not match format '%b %d %Y %H %M %S'
Example Date formats in .csv:
10/07/2016
10/07/2016 09:00

Data
newDF=pd.DataFrame({'Effective_Date':['10/07/2016','10/07/2016 09:00','09 August 2016'],'Paid_Off_Time':['10 July 2016','10/08/2016','10/09/2016 01:00:30']})
Effective_Date Paid_Off_Time
0 10/07/2016 10 July 2016
1 10/07/2016 09:00 10/08/2016
2 09 August 2016 10/09/2016 01:00:30
Solution
newDF.assign(Effective_Date=pd.to_datetime(newDF['Effective_Date']).dt.date,Paid_Off_Time=pd.to_datetime(newDF['Paid_Off_Time']).dt.date)
Effective_Date Paid_Off_Time
0 2016-10-07 2016-07-10
1 2016-10-07 2016-10-08
2 2016-08-09 2016-10-09

Checkout the Pandas documentation on Data Functionality for more details:
https://pandas.pydata.org/docs/user_guide/timeseries.html#converting-to-timestamps

Related

Pandas - converting out of order string date time

I have a DataFrame column that has string values for date/time (Input data). I need to convert it into a semi-timestamp format (Desired output data). There are rows that are blank and need to remain blank. I use quotes for illustrative purposes. I am using strptime but getting an error (see below).
Input data (String):
Mar 8 12:00 PM 2020
' '
Mar 8 1:00 PM 2020
Mar 8 6:00 PM 2020
Mar 9 8:00 AM 2020
Desired output data:
3/8/2020 12:00:00
' '
3/8/2020 13:00:00
3/8/2020 18:00:00
3/9/2020 08:00:00
Code:
import datetime as dt
df['date'].apply(lambda x: dt.datetime.strptime(x, '%b %d %H:%M %p %Y'))
Error:
ValueError: time data '' does not match format '%b %d %H:%M %p %Y'
How can I rewrite this code to get the desired output?
For me working to_datetime with format similar like yoour with %I for select hours in 12H format, also is added errors='coerce' for missing values (NaT) if some value not matching:
df['date'] = pd.to_datetime(df['date'], format='%b %d %I:%M %p %Y', errors='coerce')
print (df)
date
0 2020-03-08 12:00:00
1 NaT
2 2020-03-08 13:00:00
3 2020-03-08 18:00:00
4 2020-03-09 08:00:00
Last for custom format use Series.dt.strftime with Series.replace:
df['date'] = (pd.to_datetime(df['date'], format='%b %d %I:%M %p %Y', errors='coerce')
.dt.strftime('%m/%d/%y %H:%M:%S')
.replace('NaT', ''))
print (df)
date
0 03/08/20 12:00:00
1
2 03/08/20 13:00:00
3 03/08/20 18:00:00
4 03/09/20 08:00:00
Or replace multiple spoaces to one space:
df['date'] = (pd.to_datetime(df['date'].replace('\s+', ' ', regex=True), format='%b %d %I:%M %p %Y', errors='coerce')
.dt.strftime('%m/%d/%y %H:%M:%S')
.replace('NaT', ''))
print (df)
date
0 03/08/20 12:00:00
1
2 03/08/20 13:00:00
3 03/08/20 18:00:00
4 03/09/20 08:00:00

Changing date time format into another format

I have an output "Wed Mar 1 00:00:00 2000". I want to convert this into the format '08/11/2019 05:45PM'. How to achieve this format?
You could use something like below
import datetime
datetime.datetime.strptime('Wed Mar 1 00:00:00 2000', '%a %b %d %H:%M:%S %Y').strftime('%d/%m/%Y %I:%M%p')

Insert Text Between Two Date Variables

I am trying to insert some text between two date commands
Code:
date -u "+%H":00Z --date="1 hours ago" "+%a %b %d" --date="0 days ago"
Error:
date: extra operand ā€˜+%a %b %dā€™
Try 'date --help' for more information.
Expected Output:
11:00Z Fri Oct 20
With GNU date , in 2 steps:
date -d #$(date +%s --date="0 days ago") -u "+%H:00Z %a %b %d" --date="-1 hours ago"

Datetime Error: Time data does not match [duplicate]

This question already has an answer here:
Not able to convert "00:30 AM" to 24 hours by strptime python
(1 answer)
Closed 5 years ago.
I am trying to create date time functions using the following code:
d1 = datetime.strptime('1/1/1960 0:00 AM', '%m/%d/%Y %I:%M %p')
d2 = datetime.strptime('1/1/2000 0:00 AM', '%m/%d/%Y %I:%M %p')
I get the following error:
ValueError: time data '1/1/1960 0:00 AM' does not match format
'%m/%d/%Y %I:%M %p'
I would appreciate help with this as I have tried tweaking the parameters to no avail.
0:00 AM doesn't match %I:%M %p, because %I doesn't include the hour 0, just 1 to 12 like on an analogue clock face:
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
Midnight in a 12-hour clock is 12:00 AM:
>>> datetime.strptime('1/1/1960 12:00 AM', '%m/%d/%Y %I:%M %p')
datetime.datetime(1960, 1, 1, 0, 0)

list of date formats supported by rich:calender

I searched for the list of date formats supported by Rich:Calender in google.
FMDAY,DD MONTH YYYY and DD MON YYYY date formats when selected are not working as expected in Rich:Calender. Can someone help to find the equivalent date formats for FMDAY,DD MONTH YYYY and DD MON YYYY date formats for Rich:Calender or any source from where i can get the list of supported date format
It are just standard Java SimpleDateFormat patterns. You can find a table in its javadoc:
Letter Date or Time Component Presentation Examples
------ ---------------------- ------------------ -----------------------------
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
It's not exactly what you mean with "FMDAY", but I'll assume that you mean the text representation of day in week such as "Tuesday".
In that case, the desired FMDAY, DD MONTH YYYY format should be set as
<rich:calendar ... pattern="EEEE, dd MMMM yyyy" />
and DD MON YYYY as
<rich:calendar ... pattern="dd MMM yyyy" />
Today's date should then result in "Tuesday, 27 November 2012" and "27 Nov 2012" respectively.

Resources