Python Removing AM and PM from column date - python

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!!!

Related

How to read german date into pandas datetime?

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'

time data not matching format

time data '07/10/2019:08:00:00 PM' does not match format '%m/%d/%Y:%H:%M:%S.%f'
I am not sure what is wrong. Here is the code that I have been using:
import datetime as dt
df['DATE'] = df['DATE'].apply(lambda x: dt.datetime.strptime(x,'%m/%d/%Y:%H:%M:%S.%f'))
Here's a sample of the column:
Transaction_date
07/10/2019:08:00:00 PM
07/23/2019:08:00:00 PM
3/15/2021
8/15/2021
8/26/2021
Your format is incorrect. Try:
df["DATE"] = pd.to_datetime(df["DATE"], format="%m/%d/%Y:%I:%M:%S %p")
You should be using %I to specify a 12-hour format and %p for AM/PM.
Separately, just use pd.to_datetime instead of importing datetime and using apply.
Example:
>>> pd.to_datetime('07/10/2019:08:00:00 PM', format="%m/%d/%Y:%I:%M:%S %p")
Timestamp('2019-07-10 20:00:00')
Edit:
To handle multiple formats, you can use pd.to_datetime with fillna:
df["DATE"] = pd.to_datetime(df["DATE"], format="%m/%d/%Y:%I:%M:%S %p", errors="coerce").fillna(pd.to_datetime(df["DATE"], format="%m/%d/%Y", errors="coerce"))

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 datetime in a dataframe the way I want?

I cannot find the correct format for this datetime. I have tried several formats, %Y/%m/%d%I:%M:%S%p is the closest format I can find for the example below.
df['datetime'] = '2019-11-13 16:28:05.779'
df['datetime'] = pd.to_datetime(df['datetime'], format="%Y/%m/%d%I:%M:%S%p")
Result:
ValueError: time data '2019-11-13 16:28:05.779' does not match format '%Y/%m/%d%I:%M:%S%p' (match)
Before guessing yourself have pandas make the first guess
df['datetime'] = pd.to_datetime(df['datetime'], infer_datetime_format=True)
0 2019-11-13 16:28:05.779
Name: datetime, dtype: datetime64[ns]
You can solve this probably by using the parameter infer_datetime_format=True. Here's an example:
df = {}
df['datetime'] = '2019-11-13 16:28:05.779'
df['datetime'] = pd.to_datetime(df['datetime'], infer_datetime_format=True)
print(df['datetime'])
print(type(df['datetime'])
Output:
2019-11-13 16:28:05.779000
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
Here is the pandas.to_datetime() call with the correct format string: pd.to_datetime(df['datetime'], format="%Y/%m/%d %H:%M:%S")
You were missing a space, %I is for 12-hour time (the example time you gave is 16:28, and %p is, to quote the docs, the Locale’s equivalent of either AM or PM.

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