I'm trying to convert 01/02/20(Thu)05:05:17 into DD-MM-YYYY format, but struggling through pandas.
df['_source.now'] = pd.to_datetime(df['_source.now'])
I get this error: ParserError: Unknown string format: 01/02/20(Thu)05:05:17
You can try this
# day first, use '%d/%m/%y(%a)%H:%M:%S'
# month first, use '%m/%d/%y(%a)%H:%M:%S'
pd.to_datetime(df['_source.now'],format='%m/%d/%y(%a)%H:%M:%S',errors='coerce').dt.strftime('%d-%m-%Y')
Related
I am trying to convert some data from a .txt file to a dataframe to use it for some analysis
the form of the data in the .txt is a follows
DATE_TIME VELOC MEASURE
[m/s] [l/h]
A 09.01.2023 12:45:20 ??? ???
A 09.01.2023 12:46:20 0,048 52,67
A 09.01.2023 12:47:20 0,049 53,77
A 09.01.2023 12:48:20 0,050 54,86
I load the data to a dataframe no problem i covnert the str values of the measurement to float etc everything is good as shows in the
image
the problem I get is when trying to convert the column of the date time that is string to datetime pandas format using this line of code:
volume_flow['DATE_TIME'] = pd.to_datetime(volume_flow['DATE_TIME'], format = '%d.%m.%Y %H:%M:S')
and i get the following error
ValueError: time data '09.01.2023 12:46:20' does not match format '%d.%m.%Y %H:%M:S' (match)
but i don't see how the format is off
I am really lost as to why this is caused as i used the same code with different formats of datetime before with no problem
further more i tried using format = '%dd.%mm.%yyyy %H:%M:S' as well with the same results and when i let the pandas.to_datetime convert it automatically it confuses the day and the month of the data. the data is between 09.01-12.01 so you can't really tell if one is the month or day just by the values.
I think you should go from this
(..., format='%d.%m.%Y %H:%M:S')
to this
(..., format='%d.%m.%Y %H:%M:%S')
You forgot the percentage character!
check the documentations for correct time format. You will note that the directive %S represents the seconds.
Second as a decimal number [00,61].
I have a string in dd-MON-yy format. While converting to date in python, its is causing issue since the year is in tow digits.
datetime.datetime.strptime('17-JUN-03', '%d-%m-%y')
The error is,
ValueError: time data '17-JUN-03' does not match format '%d-%m-%y'
Try this:
import datetime
print(datetime.datetime.strptime('17-JUN-03', '%d-%b-%y'))
Result:
2003-06-17 00:00:00
Datetime format codes
I'm trying to convert dates in the format '31-Aug-91' into '31-08-1991' using pandas datetime.
I've tried pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%m-%y').dt.date
But, I get the error ValueError: time data '31-Aug-91' does not match format '%d-%m-%y' (match)
How do I fix this?
Use %b for match first 3 letters of month names:
pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.date
If need format DD-MM-YYYY:
pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.strftime('%d-%m-%Y')
I have Date/Time in the following format:
10/01/21 04:49:43.75
MM/DD/YY HH/MM/SS.ms
I am trying to convert this from being an object to a datetime. I tried the following code but i am getting an error that it does not match the format. Any ideas?
df['Date/Time'] = pd.to_datetime(df['Date/Time'], format = '%m%d%y %H%M%S%f')
you can try letting pandas infer the datetime format with:
pd.to_datetime(df['Date/Time'], infer_datetime_format=True)
I want to reformat the timestamp in my dataset to have it as a date + time.
here is my dataset
and I tried this
data1 = pd.read_excel(r"C:\Users\user\Desktop\Consumption.xlsx")
data1['Timestamp']= pd.to_datetime(['Timestamp'], unit='s')
and I got this error
ValueError: non convertible value Timestamp with the unit 's'
I also tried not to pass the "unit" in the pd.to_datetime function and it gave an error
The type of time stamp is Object. Please any help.
Format of datetimes is not unix time, so raised error. You can split values by ; and select second lists by str[1] and then convert to datetimes:
data1['Timestamp']= pd.to_datetime(data1['Timestamp'].str.split(';').str[1])
I would suggest you check the documentation of the function here
If you want to add date-time, you can format like this:
format='%d/%m/%Y %H:%M:%S'
Try this:
data1['Date'] = pd.DataFrame(data1['Timestamp'], format ='%d/%m/%Y')