Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing all dates to standard date time in dataframe

I have a dataframe with date column where it looks like this. There are more than one date column such as end date, fiscal year date etc.

Plan Start Date
8/16/2017 0:00
5/31/2017 0:00
5/31/2017 0:00
5/31/2017 0:00
5/31/2017 0:00
4/21/2016 0:00
2/25/2016 0:00
12/15/2016 0:00
12/15/2016 0:00
12/15/2016 0:00
42373
42373
42367
42367
42367
42367
42460
42460
42460
42460
42460
42759
42333

I am trying to write a function where it basically changes those integrers to appropriate date format and format this column as datetime[64]. this column format is current object type.

I have written below function

def change_date_df(df):
    format_dates_df = [col for col in df.columns if 'Date' in col];
    for date in format_dates_df:
        df[date] = pd.to_datetime(df[date]).apply(lambda x: x.strftime('%d-%m-%y')if not pd.isnull(x) else '');
    return df;

Its giving back now a

ValueError: mixed datetimes and integers in passed array

Im guessing these numbers are not being converted to dates. but Im not sure how else i can adjust my code.

Any idea?

Adam

like image 937
Adam Avatar asked Jan 10 '18 05:01

Adam


People also ask

How do I change the date format in a data frame?

Function usedstrftime() can change the date format in python.

How do I change date and time format in python?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

How do I change date format to MM DD YYYY in pandas?

Please notice that you can also specify the output date format other than the default one, by using the dt. strftime() method. For example, you can choose to display the output date as MM/DD/YYYY by specifying dt. strftime('%m/%d/%Y') .

What is the method in pandas to change values into date time data type?

Code #1 : Convert Pandas dataframe column type from string to datetime format using pd. to_datetime() function.


1 Answers

Referencing How to convert a given ordinal number (from Excel) to a date, convert the ordinal values to datetime using from_excel_ordinal -

m = df['Plan Start Date'].str.isdigit()

Or, if you have a column of objects -

df['Plan Start Date'].astype(str).str.isdigit()

Next, apply the function on a subset of the rows using apply -

df.loc[m, 'Plan Start Date'] = \
df.loc[m, 'Plan Start Date']\
  .astype(int)\
  .apply(from_excel_ordinal)

Finally, convert the entire column to datetime using pd.to_datetime, giving a uniform result -

df['Plan Start Date'] = pd.to_datetime(df['Plan Start Date'], errors='coerce')

df

   Plan Start Date
0       2017-08-16
1       2017-05-31
2       2017-05-31
3       2017-05-31
4       2017-05-31
5       2016-04-21
6       2016-02-25
7       2016-12-15
8       2016-12-15
9       2016-12-15
10      2016-01-04
11      2016-01-04
12      2015-12-29
13      2015-12-29
14      2015-12-29
15      2015-12-29
16      2016-03-31
17      2016-03-31
18      2016-03-31
19      2016-03-31
20      2016-03-31
21      2017-01-24
22      2015-11-25
like image 200
cs95 Avatar answered Sep 24 '22 05:09

cs95