How to read german date into pandas datetime? - python

I have an excel file with a column date like these:
28.02.2022 00:00:00
What I want is to get it to datetime format without the hour.
I use this and this gives me an error "time data does not match data":
df['date'] = pd.to_datetime(df['date'], format='%d.%m.%Y %H:%M:%Y')
I can't find my error since the format seems to be right. I really appreciate every help.:)

I suspect your format= should be
'%d.%m.%Y %H:%M:%S'
and not
'%d.%m.%Y %H:%M:%Y'
(which has a duplicate %Y).
>>> pd.to_datetime('28.02.2022 00:00:00', format='%d.%m.%Y %H:%M:%S')
Timestamp('2022-02-28 00:00:00')
Whether or not you care about the time part in the data is another thing.

You can try:
pd.to_datetime(df['date']).date()
Something like:
str(pd.to_datetime('28.02.2022 00:00:00').date())
would return:
'2022-02-28'

Related

Python, CSV file only shows mm.ss and not hh.mm.ss

In my csv file, the values for a column are 1/1/2022 12:34:16am. However, the value in the cell only shows 34:16.02 (minutes, seconds, and milliseconds).
I would like to convert this column to 1/1/2022 12:34:16am in datetime format so I can subtract another similar column to get the time difference.
I have tried to use strptime but it gives me an error that 'values must be in string format'. So I tried to convert the values to 'str' but that still does not work.
df['start'] = df['start'].astype("str")
df['start'] = datetime.strptime(df['start'], '%d/%m/%y %H:%M:%S')
Anyone able to help? Thanks alot!
Try the following:
date_string = str(df['start'])
format = '%d/%m/%Y %H:%M:%S%p'
df['start'] = datetime.strptime(date_string, format)

Python Removing AM and PM from column date

Hello I tried different code to remove AM/MP from csv file in python (pandas).
date_time
5/5/2014 7:42:39 AM
I used following code but UNFORTUNATELY nothing changes. Could you please let me know how can I get ride of PM/AM from column date in pandas?
df['TimeStamp'] = pd.to_datetime(df['TimeStamp'], format="%m/%d/%Y %I:%M:%S %p")
Colud you please let me know what shall I do to remove them from column date?
Try if this works:
df['TimeStamp'] = pd.to_datetime(df['TimeStamp'], format="%m/%d/%Y %I:%M:%S %p")
df['TimeStamp'] = df['TimeStamp'].dt.strftime("%m/%d/%Y %H:%M:%S")
Edit: I just noticed you were using double quotes here "%m/%d/%Y %I:%M:%S %p"
try using single quotes
df['TimeStamp'] = pd.to_datetime(df['TimeStamp'], format='%m/%d/%Y %I:%M:%S %p')
It might be because the column is coming in as a string. If it is, you could try removing the last three strings (AM/PM and the space before it) and then convert to date and time:
df['TimeStamp'] = df['TimeStamp'].str[:-3]
df['TimeStamp']= pd.to_datetime(df['TimeStamp'])
Check the data type first though:
print(df['TimeStamp'].dtypes)
Thanks for your answers and comments. None of them cannot remove AM/PM, I just changed time format in control panel!!!!! it done!!!

Python Pandas converting datestring to datetime

I have a datestring in thew following format which is month/day/year and then time
print(df):
Date
6/06/20 4:41pm
6/06/20 5:41pm
I am trying to convert using pd.to_datetime and have used the following:
df['Date'] = pd.to_datetime(df['Date'], format='%m%d%Y:%H:%M.%f')
but I cant match the format. Does anyone know the format for this particular sting? thank you very much!
Data
df=pd.DataFrame({'Date':['6/06/20 4:41pm','6/06/20 5:41pm']})
df['Date']=pd.to_datetime(df['Date'])
you are missing part of the format
df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%Y %H:%M.%f')

How to format date to 1900's?

I'm preprocessing data and one column represents dates such as '6/1/51'
I'm trying to convert the string to a date object and so far what I have is:
date = row[2].strip()
format = "%m/%d/%y"
datetime_object = datetime.strptime(date, format)
date_object = datetime_object.date()
print(date_object)
print(type(date_object))
The problem I'm facing is changing 2051 to 1951.
I tried writing
format = "%m/%d/19%y"
But it gives me a ValueError.
ValueError: time data '6/1/51' does not match format '%m/%d/19%y'
I couldn't easily find the answer online so I'm asking here. Can anyone please help me with this?
Thanks.
Parse the date without the century using '%m/%d/%y', then:
year_1900 = datetime_object.year - 100
datetime_object = datetime_object.replace(year=year_1900)
You should put conditionals around that so you only do it on dates that are actually in the 1900's, for example anything later than today.

Python/Pandas Datetime Conversion of Date in Format DD-MON-YY HH:MM:SS.NS PM

Running into some issues with datetime conversion.
import pandas as pd
mydate = '12-AUG-03 04.16.41.000000 PM'
mydateconv = pd.to_datetime(mydate)
print mydateconv
"2003-08-12 12:00:00"
Is there a reason the time is being reset to 12:00:00?
I've also tried other formatting derivations without success.
mydateconv = pd.to_datetime(mydate, format = '%d-%m-%y %I:%M:%S.%f %p')
Any recommendations?
The raw data I'm receiving has dates in the above format, so I'm looking for suggestions on a solution which addresses dates in this format, whether it's the use of stock function or determination that I'll need something a bit more custom because of the format.
Many thanks in advance for any thoughts.
Your format string needs to be: '%d-%b-%y %I.%M.%S.%f %p', see the docs:
In [35]:
pd.to_datetime('12-AUG-03 04.16.41.000000 PM', format = '%d-%b-%y %I.%M.%S.%f %p')
Out[35]:
Timestamp('2003-08-12 16:16:41')
You had several errors in your format string '%d-%m-%y %I:%M:%S.%f %p'.
Firstly your months are abbreviated so you should use b instead of m.
Secondly your time components had dot (.)separators not colon (:) separators.

Categories

Resources