taga
taga

Reputation: 3885

Mixing days and months in datetime value column in Pandas

I want to format dates in pandas, to have year-month-day. My dates are from april to september. I do not have values from january, feb etc, but sometimes my pandas reads day as month and month as day. Look at index 16 or 84.

6    2019-08-26 15:10:00
7    2019-08-25 13:22:00
8    2019-08-24 16:06:00
9    2019-08-23 15:13:00
10   2019-08-22 14:24:00
11   2019-08-21 14:02:00
12   2019-08-16 12:31:00
13   2019-08-15 15:31:00
14   2019-08-14 14:46:00
15   2019-08-13 17:13:00
16   2019-11-08 15:54:00 
17   2019-10-08 10:07:00
68   2019-06-06 11:22:00
69   2019-05-06 15:16:00
70   2019-01-06 17:02:00
75   2019-05-21 09:01:00
76   2019-05-19 16:52:00
77   2019-05-15 15:40:00
78   2019-10-05 13:34:00
81   2019-06-05 11:55:00
82   2019-03-05 17:28:00
83   2019-02-05 18:01:00
84   2019-01-05 17:05:00
85   2019-01-05 09:57:00
86   2019-04-30 10:16:00
87   2019-04-29 17:51:00
88   2019-04-27 17:42:00

How to fix this? I want to have date type values *(year-month-day), without time, so that I can group by day, or by month.

I have tried this, but It does not work:

df['Created'] = pd.to_datetime(df['Created'], format = 'something')

And for grouping by month, I have tried this:

df['Created'] = df['Created'].dt.to_period('M')

Upvotes: 3

Views: 1431

Answers (2)

Ankita Mehta
Ankita Mehta

Reputation: 600

I created a dummy dataframe to parse this. Try strftime

from datetime import datetime
import time
import pandas as pd

time1 = datetime.now()
time.sleep(6) 
time2 = datetime.now()


df = pd.DataFrame({'Created': [time1, time2]})

df['Created2'] =  df['Created'].apply(lambda x: x.strftime('%Y-%m-%d'))

print(df.head())

Upvotes: 1

jezrael
jezrael

Reputation: 862511

Solution for sample data - you can create both possible datetimes with both formats with errors='coerce' for missing values in not match and then replace missing values from second Series (YYYY-DD-MM) by first Series (YYYY-MM-DD) by Series.combine_first or Series.combine_first:

a = pd.to_datetime(df['Created'], format = '%Y-%m-%d %H:%M:%S', errors='coerce')
b = pd.to_datetime(df['Created'], format = '%Y-%d-%m %H:%M:%S', errors='coerce')

df['Created'] = b.combine_first(a).dt.to_period('M')
#alternative
#df['Created'] = b.fillna(a).dt.to_period('M')

print (df)
    Created
6   2019-08
7   2019-08
8   2019-08
9   2019-08
10  2019-08
11  2019-08
12  2019-08
13  2019-08
14  2019-08
15  2019-08
16  2019-08
17  2019-08
68  2019-06
69  2019-06
70  2019-06
75  2019-05
76  2019-05
77  2019-05
78  2019-05
81  2019-05
82  2019-05
83  2019-05
84  2019-05
85  2019-05
86  2019-04
87  2019-04
88  2019-04

Upvotes: 2

Related Questions