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.
Related
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')
I am getting a date from an API that I am trying to pass to my template after formatting using datetime but I keep getting this error:
time data 2021-03-09T05:00:00.000Z does not match format %Y-%m-%d, %I:%M:%S;%f
I know I have to strftime and then strptime but I cant get past that error.
I would like to split it into two variables one for date and one for the time that will show in the users timezone.
date = game['schedule']['date']
game_date = datetime.strptime(date, '%Y-%m-%d, %I:%M:%S;%f')
You have a slightly wrong time format:
game_date = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ')
Or (if you remove the last Z character from the date string) you can also use datetime.fromisoformat:
game_date = datetime.fromisoformat(date[:-1])
And then you can extract date and time this way:
date = game_date.date()
time = game_date.time()
time_with_timezone = game_date.timetz()
I am using the below reg.sub to change the date format from yyy-mm-dd to dd-mm-yyy . But the new date format also comes in the original format and does not change.Can you please point me what I am missing here?
def dateextract1(dt):
return re.sub(r'(\d{4})-(\d{1,2})-(\d{1,2})/', '\\3-\\2-\\1', dt)
dt1 = "2026-01-02"
print("Original date in YYY-MM-DD Format: ",dt1)
print("New date in DD-MM-YYYY Format: ",dateextract1(dt1))
=======================
Original date in YYY-MM-DD Format: 2026-01-02
New date in DD-MM-YYYY Format: 2026-01-02 [ I would expect : 02-01-2026]
Any reason for using RegEx for this? The Python datetime library has a pretty effective date formatter.
My data 'df' shows data 'Date' as 1970-01-01 00:00:00.019990103 when this is formatted to date_to using pandas. How do I show the date as 01/03/1999?
consider LoneWanderer's comment for next time and show some of the code that you have tried.
I would try this:
from datetime import datetime
now = datetime.now()
print(now.strftime('%d/%m/%Y'))
You can print now to see that is in the same format that you have and after that is formatted to the format required.
I see that the actual date is in last 10 chars of your source string.
To convert such strings to a Timestamp (ignoring the starting part), run:
df.Date = df.Date.apply(lambda src: pd.to_datetime(src[-8:]))
It is worth to consider to keep this date just as Timestamp, as it
simplifies operations on date / time and apply your formatting only in printouts.
But if you want to have this date as a string in "your" format, in the
"original" column, perform the second conversion (Timestamp to string):
df.Date = df.Date.dt.strftime('%m/%d/%Y')
I am attempting to create a time series index using pandas. Currently this is the code I am running:
date_string = df3["Date"]
date_times = pd.to_datetime(date_string, yearfirst=True, format='%Y%m%d%H%M')
df3_i = df3.set_index(date_times)
Yet I am getting constant errors. Can anyone explain?
Error:
ValueError: time data '2017-03-08 13:53' does not match format '%Y%m%d%H:%M' (match)
That's because the format is '%Y-%m-%d %H:%M'
There are special character combinations that are meant to represent the numeric components of the date and time. A great reference can be found here
You have a time string of '2017-03-08 13:53' as evidenced by you error message. From the link you'll find that:
4 digit year is '%Y'
2 digit month is '%m'
2 digit day is '%d'
2 digit hour is '%H'
2 digit minute is '%M'
So you still need to represent the other string bits like the dashes, space, and the colon
Thus '%Y-%m-%d %H:%M'
Use this instead
date_string = df3["Date"]
date_times = pd.to_datetime(date_string, yearfirst=True, format='%Y-%m-%d %H:%M')
df3_i = df3.set_index(date_times)
If that doesn't work, then you have inconsistent date formats and my first course of action would be to yell at whoever created the thing I'm trying to parse.
If that happens to be your scenario, ask another question... Or I might.