Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I've taken my Series and coerced it to a datetime column of dtype=datetime64[ns] (though only need day resolution...not sure how to change).

import pandas as pd

df = pd.read_csv('somefile.csv')

column = df['date']

column = pd.to_datetime(column, coerce=True)

but plotting doesn't work:

ipdb> column.plot(kind='hist')

*** TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('float64')

I'd like to plot a histogram that just shows the count of dates by week, month, or year.

Surely there is a way to do this in pandas?

1 Answer

0 votes
by (41.4k points)

Given this df:

        date

0 2001-08-10

1 2002-08-31

2 2003-08-29

3 2006-06-21

4 2002-03-27

5 2003-07-14

6 2004-06-15

7 2003-08-14

8 2003-07-29

and, if it's not already the case:

df["date"] = df["date"].astype("datetime64")

To show the count of dates by month:

df.groupby(df["date"].dt.month).count().plot(kind="bar")

.dt allows you to access the datetime properties.

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 31, 2019 in Data Science by sourav (17.6k points)

Browse Categories

...