Python Pandas converting datestring to datetime - python

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')

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'

Problem converting time into pandas datetime

I am trying to convert a date column containing only hours, minutes and seconds ito a datetime form using pandas.to_datetime(). However, it adds year and date automatically. I also tried using
pandas.to_datetime(df["time"], format = %H:%M:%S").dt.time, again the data type remains object.
Is there any method that can change into datetime format without year and date?
Something like this?
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S', errors='ignore')
put .dt.time on the end
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S', errors='ignore').dt.time

change timestamp format pandas

Example dataHow to change the timestamp format which has the format of '2019-12-16-12-40-53' and I want it to convert to '2019-12-16 12:40:53'
I tried
df['timeStamp'] = df['timeStamp'].apply(lambda x:
dt.datetime.strptime(x,'%Y%b%d:%H:%M:%S'))
and I got the error
ValueError: time data '2019-12-16-12-40-53' does not match format '%Y%b%d:%H:%M:%S'
I am attaching an image of data that I am using as timestamp.
Use to_datetime and change format with %m for match months in numbers with - between parts of datetimes:
df['timeStamp'] = pd.to_datetime(df['timeStamp'], format='%Y-%m-%d-%H-%M-%S')

why strftime convert datetime to object format?

i converted date from y/m/d to d/m/y format in a data-frame using
from pandas import pd
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d ').dt.strftime('%d/%m/%Y')
but after this code execute the df['Date'] field become object format,when i try to convert datetimeformat it again become y/m/d form ,is there any solution for this?

What is the correct pd.to_datetime format for strings of the form '12/2/14 0:01'?

I'm trying to convert the column 'DateTime' of my pandas df to DateTime format. The following doesn't work, how can I specify the format when my strings are of the form '12/2/14 0:01'?
df['DateTime'] = pd.to_datetime(df['DateTime'], format='%m/%d/%YY %H:%M').dt.time
Better yet, can I simply break it apart into two separate columns for date and time?
from datetime import datetime
df['DateTime'] = df['DateTime'].apply(lambda x: datetime.strptime(x, '%m/%d/%y %H:%M'))

Categories

Resources