I have a pandas dataframe with a datetime column. I would like to plot the distribution of the rows according to that date column, but I\'m currenty getting an unhelpful error.
You could convert the dates to Categorical type, and plot the resulting codes (which are integers). Then, label the x-ticks with the Date (as category).
import pandas as pd
import seaborn as sns
original_dates = [
"2016-03-05", "2016-03-05", "2016-02-05",
"2016-02-05", "2016-02-05", "2014-03-05"]
dates_list = pd.to_datetime(original_dates)
df = pd.DataFrame({"Date": dates_list})
df['date-as-cat'] = df['Date'].astype('category') # new
df['codes'] = df['date-as-cat'].cat.codes # new
print(df)
print(df.dtypes)
Date date-as-cat codes
0 2016-03-05 2016-03-05 2
1 2016-03-05 2016-03-05 2
2 2016-02-05 2016-02-05 1
3 2016-02-05 2016-02-05 1
4 2016-02-05 2016-02-05 1
5 2014-03-05 2014-03-05 0
Date datetime64[ns]
date-as-cat category
codes int8
dtype: object
The date-as-code and date-as-category info is obtained like this:
x = df[['codes', 'date-as-cat']].drop_duplicates().sort_values('codes')
print(x)
codes date-as-cat
5 0 2014-03-05
2 1 2016-02-05
0 2 2016-03-05