I am working with the below DateTime data:
DateTime
13/09/2018 6:16:58 PM
22/09/2018 5:16:07 PM
23/09/2018 10:52:54 AM
23/09/2018 3:01:12 AM
23/09/2018 5:47:44 AM
23/09/2018 7:06:44 PM
23/09/2018 8:46:25 AM
3/9/2018 16:00
3/9/2018 19:39
30/09/2018 9:52:35 PM
6/9/2018 15:49
6/9/2018 16:15
6/9/2018 16:41
6/9/2018 20:02
7/9/2018 13:49
7/9/2018 20:43
7/9/2018 22:26
7/9/2018 9:32
8/9/2018 1:12
8/9/2018 14:24
8/9/2018 16:18
9/9/2018 13:45
As it can be seen the format of DateTime in this column is mixed. I tried the following piece of code to standardize the format and extract some results out of that data:
#Converting the column into DateTime objects
pf['DateTime'] = pd.to_datetime(pf['DateTime'], errors = 'coerce')
# Set up a standard DateTime like format throughout the column
# pf is the data frame
pf['DateTime'].dt.strftime("YYYY-MM-DD hh:mm:ss")
# Grouping the DateTime column by Month
grp = pf.groupby(pf['DateTime'].dt.to_period('M'))
for d in grp:
print('d',d)
But strangely, even after setting the format of the DateTime columns, the dates are being picked out as month and the groups I am getting are :
d (Period('2018-03', 'M')
d (Period('2018-06', 'M')
d (Period('2018-07', 'M')
d (Period('2018-08', 'M')
d (Period('2018-09', 'M')
The fact is the values 6,7,8 are the dates of month 9 and are mistakenly picked up as month. The expected outcome should be:
d (Period('2018-09', 'M')
How to set up the columns in such a way that it doesn't pick the date as a month.
strapi of Pandas andrstriporreplaceto remove AM and PM