converting date string to timestamp - python

I have date strings like '1997-05-07 ' ('year-month-day') and when I try to convert it to timestamp (in order to compare them and have some plots I need to convert them ) I get this error :
ValueError: time data '1997-07-05' does not match format '%y-%m-%d'
what I try is :
time.mktime(datetime.datetime.strptime('1997-07-05','%y-%m-%d').timetuple())

The %y format expects the year without century as a decimal padded number. If you want to parse 1997, you need to use %Y.
You can view more information on how strptime works in the official documentation.

You have your format wrong - %y matches only 2-digit year. If you want "full" year, you have to use %Y:
time.mktime(datetime.datetime.strptime('1997-07-05','%Y-%m-%d').timetuple())
868053600.0

Related

Pandas.to_datetime doesn't recognize the format of the string to be converted to datetime

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].

Change date format of these string using Python

I have a string from a pdf that I want to transform it to the date format that I want to work with later,
the string is
05Dec22
how can I change it to 12/05/2022?
import datetime
date1 = '05Dec22'
date1 = datetime.datetime.strptime(date1, '%d%m%Y').strftime('%m/%d/%y')
date1 = str(date1)
This is what i tried so far
If you execute the code you'll get the following error,
ValueError: time data '05Dec22' does not match format '%d%m%Y'
this is because your time string is not in the specified format given ('%d%m%Y'). You can search for tables on the internet which show the placeholders that represent a certain formatting, if you look at the one provided here, you'll see that the formatting your string has is '%d%b%y', in this case, the %b placeholder represents the abbreviated month name and the %y placeholder is the year without century, just as your example string. Now, if you fix that in your code,
import datetime
date1 = '05Dec22'
date1 = datetime.datetime.strptime(date1, '%d%b%y').strftime('%m/%d/%Y')
date1 = str(date1)
you'll get the desired result.
Note that you also have to change the output format in strftime. As I said before, the %y placeholder is the year without century. For you to get the year including the century, you have to use %Y.

I keep getting a date format error when trying to convert an int into a date

So I currently have a dataset that has a date column as an int, for example 102809 as 10/28/2009. So I'm trying to convert this column into an actual date column that would have the correct date format.
I tried this code from another question on stack overflow:
curry['Datetime'] = curry['Date'].apply(lambda x: pd.to_datetime(str(x), format='%m/%d/%Y'))
But this line returns this error:time data '102809' does not match format '%m/%d/%Y' (match)
Change it to :
curry['Datetime'] = curry['Date'].apply(lambda x: pd.to_datetime(str(x), format='%m%d%y'))
I have removed the slashes and change the %Y to lower case -> %y, because it is only two digits year
It looks like your date format is two-digits each for month, day and year, so your format should be '%m%d%y' with no slashes. (A two-digit year is %y not %Y.) Also, there is no need to use df.apply() which is slower.
curry['Datetime'] = pd.to_datetime(curry['Date'], format='%m%d%y')

Time data does not match format - ValueError

I am trying to change string to datetime like below:
max_datetime = datetime.strptime(max_date,'%y-%m-%d %H:%M:%S')
However, I am getting the below-mentioned error:
ValueError: time data '2008-05-15 11:26:40' does not match format '%y-%m-%d %H:%M:%S'
Any help will be appreciated!
The documentation of datetime tells that %y (with a lowercase y) represents a two-digit year, while from the error-message we can see that your input, max_date has a four-digit year. A four-digit year is represented by %Y (with an uppercase Y). So this is the source of your error. Since the rest looks fine,
max_datetime = datetime.strptime(max_date, "%Y-%m-%d %H:%M:%S")
should do the job.

Is there a way to convert string to a date with this format (2-Feb-19) in python?

I am trying to convert a string into a date before I can do some arithmetic on the date.
My string has this format 2-FEB-19, I want to keep the same format after converting it to a date. How can I do that?
I tried df['dates'].to_datetime('2-Feb-19', format='%d-%m-%y') and I got an error message "time data '2-Feb-19' does not match format '%d-%m-%y' (match)"
You are using %m, which wants a month as a zero-padded number, such as 02. use %b.
It looks from your question like you are dealing with a single string rather than a Pandas Series or other sequence. For that, you can use strptime() from Python's datetime module in the standard library:
>>> datetime.datetime.strptime('2-FEB-19', '%d-%b-%y')
datetime.datetime(2019, 2, 2, 0, 0)
If you do really want to use Pandas, whether it be on a scalar (string) or sequence, you may want to use the %b format specifier:
>>> pd.to_datetime('2-FEB-19', format='%d-%b-%y')
Timestamp('2019-02-02 00:00:00')
Python's reference on strptime directives is at the datetime docs.
Granted, Pandas (and the underlying date-parsing libraries it uses) are pretty good at inference, and will usually guess things right:
>>> pd.to_datetime('2-FEB-19')
Timestamp('2019-02-02 00:00:00')
You should use the appropriate format:
df['dates'].to_datetime('2-Feb-19', format='%d-%b-%y')
%b "Month as locale’s abbreviated name": Jan, Feb, …, Dec (en_US);
Ref. https://docs.python.org/3/library/datetime.html

Categories