I want to convert (01-07-57) to date value - python

I want to convert the datum column which is a string to DateTime format.
when I use
sf['Datum'] = pd.to_datetime(sf['Datum']).dt.date
its showing the year as 2057 instead of 1957

There's a answer over here:
pandas to_datetime parsing wrong year
It's due to 2 digit years from 0-68 mapping to 20xx.

what if just replace years?
sf['Datum'] = pd.to_datetime(sf['Datum'].str.replace(r'(\d\d)$',r'19\1'))

Related

Read excel dates and convert them to individual strings [duplicate]

This question already has answers here:
Extract day and month from a datetime object
(4 answers)
Closed 12 months ago.
I recently started using python.
I have a series of dates in excel
01-05-2021
02-05-2021
.
.
29-05-2021
Now, I want to load this column and convert it into individual strings based on rows. So i can extract the day, month and year separately for each dates
Can someone help me how to do that??
you can do:
df = pd.read_excel("filename.xlsx")
# let's imagine your date column name is "date"
df["day"] = df["date"].apply(lambda elem:elem.split("-")[0])
df["month"] = df["date"].apply(lambda elem:elem.split("-")[1])
df["year"] = df["date"].apply(lambda elem:elem.split("-")[2])
from datetime import datetime
str_time = 01-05-2021
time_convert = datetime.strptime(str_time, '%d-%m-%Y')
print (time_convert, time_convert.day, time_convert.month, time_convert.year)
in your case, make the convert in looping for each data you got from the excel file

Convert 8 digits integer to standard date format in Pandas

For a date column which is like 20190101, 20190102, how could I change it to 2019/01/01, 2019/01/02 or 2019-01-01, 2019-01-02? Thanks for your help.
I have tried with df['date'] = df['date'].dt.strftime('%Y%m%d'), but it doesn't work.
Using
df['date']=pd.to_datetime(df['date'],format='%Y%m%d')

Manipulating Series in Dataframe using Pandas [duplicate]

This question already has answers here:
Pandas filter dataframe rows with a specific year
(2 answers)
Closed 3 years ago.
I have a date column in a data frame that looks like this:
(Year-Month-Day)
2017-09-21
2018-11-25
I am trying to create a function that considers only the year, I have been trying the following.
df[df['DateColumn'].str[:3]=='2017']
But I am receiving this error:
AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
How can I only consider the first four characters of the date in a function? Thanks.
I think you are looking for:
df['year'] = [d.year for d in df['DateColumn']]
This works only if the elements of the column are pandas.tslib.Timestamp. If not then :
df['DateColumn'] = pd.to_datetime(df['DateColumn'])
df['year'] = [d.year for d in df['DateColumn']]
UPDATE: Use this instead:
df.loc[pd.to_datetime(df['DateColumn']).dt.year == 2017]
According to this:
https://pandas.pydata.org/pandas-docs/stable/getting_started/basics.html#dt-accessor
If you have a Series in a DateTime format, you should be able to use the dt accessor.
So you might be able to do something like this:
df[df.dt.year == 2017]
Try:
df = pd.to_datetime(df.col).apply(lambda x: x.year)
This converts col into datetime format, then extracts year from it to make it a series.

How can I handle wrong year format

Being new to python and pandas, I faced next problem.
In my dataframe i have column with dates (yyyy-mm-ddThh-mm-sec), where most part of the years are ok (looks like 2008), and a part, where year is written like 0008. Due to this I have problem with formatting column using pd.to_datetime.
My thought was to convert it first into 2-digit year (using pd.to_datetime(df['date']).dt.strftime('%y %b, %d %H:%M:%S.%f +%Z')), but I got an error Out of bounds nanosecond timestamp: 08-10-02 14:41:00.
Are there any other options to convert 0008 to 2008 in dataframe?
Thanks for the help in advance
If the format for the bad data is always the same (as in the bad years are always 4 characters) then you can use str:
df = pd.DataFrame({'date':['2008-01-01', '0008-01-02']})
df['date'] = pd.to_datetime(df['date'].str[2:], yearfirst=True)
date
0 2008-01-01
1 2008-01-02

Change date format of dataframe date column [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Closed 4 years ago.
I have this panda dataframe df.
Name Date Score Score2
Joe 26-12-2007 53.45 53.4500
Joe 27-12-2007 52.38 52.7399
Joe 28-12-2007 51.71 51.8500
I would like to convert the date format in the Date column from dd-mm-yyyy to yyyy-mm-dd. The converted dataframe will look like this;
Name Date Score Score2
Joe 2007-12-26 53.45 53.4500
Joe 2007-12-27 52.38 52.7399
Joe 2007-12-28 51.71 51.8500
I am using python v3.6
EDIT: The duplicate question assumes that the original date format is yyyy-mm-dd. However, my original date format is dd-mm-yyyy. If I were to apply the answer in that question, the converted dates is wrong.
How to change the datetime format in pandas
Use:
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
I think you need this:
df['Date'] = df['Date'].dt.strftime('%Y-%m-%d')

Categories