change timestamp format pandas - python

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

Related

group by with year of the date

I have a date column in excel,with year_month_day format I want to extract only year of my date and group the column by year,but I got an error
df.index = pd.to_datetime(df[18], format='%y/%m/%d %I:%M%p')
df.groupby(by=[df.index.year])
18 is index of my date column
error=ValueError: time data '2022/04/23' does not match format '%y/%m/%d %I:%M%p' (match)
I don't know how can I fix it.
By the looks of it, the error message indicates that the format string you are using, %y/%m/%d %I:%M%p, doesn't match the format of the dates in your column.
It appears that your date format is YYYY/MM/DD, but the format string you're using is trying to parse it as YY/MM/DD %I:%M%p.
I think you should change the format string to %Y/%m/%d.
df.index = pd.to_datetime(df[18], format='%Y/%m/%d')
Then you can extract the year using the year attribute of the datetime object, and group by the year as you are doing.
Make sure your date column is formatted correctly. I provide here a code with which you can adjust the format of the dates.
import pandas as pd
df = pd.DataFrame({'date': ['2022/04/23', '2022/04/24', '2022/04/25']})
df['date'] = pd.to_datetime(df['date'], format='%Y/%m/%d')

How to convert a "yyyy-MM-dd'T'HH:mm:ssZ'" format in a dataframe to a datetime format

How can I convert a "yyyy-MM-dd'T'HH:mm:ssZ'" format in a dataframe to a datetime format that I can further format to an index
2021-01-02T05:22:58.000Z is one of the dates in the dataframe
i've tried this line of code:
df['created_at_tweet']= pd.to_datetime(df['created_at_tweet'], format=("yyyy-MM-dd'T'HH :mm:ss.SSS'Z'"))
but i get the error
ValueError: time data '2021-01-02T01:43:32.000Z' does not match format 'yyyy-MM-dd'T'HH :mm:ss.SSS'Z'' (match)
any ideas?
This works
df = pd.DataFrame({'created_at_tweet' : ['2021-01-02T01:43:32.000Z'], 'tweet' : ['Hello Twitter!']})
df['created_at_tweet']= pd.to_datetime(
df['created_at_tweet'],
format=('%Y-%m-%dT%H:%M:%S.%f'))
yields
df

Timestamp datetime64 to datetime in dataframe

I am confused with datetime64 and trying convert it to a normal time format.
I have a column with timestamp format: 2022.01.02D23:10:12.197164900.
Output expected is: 2022-01-02 23:10:12
I'm trying with:
df['executionTime'] = pd.to_datetime(df['executionTime'], format='%Y-%m-%d %H:%M:%S.%f', errors='coerce')
Try this:
df['executionTime'] = pd.to_datetime(df['executionTime'], format='%Y.%m.%dD%H:%M:%S.%f', errors='coerce')

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

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

Categories

Resources