Nasri
Nasri

Reputation: 545

plot with datetime type in python

I try to plot the distribution of "time" feature . Here is my dataframe :

date    amount
40  2018-12-31  104.588094
184     2019-01-01  2487.714459
80  2019-01-02  250.405264
10  2019-01-03  217.385350
170     2019-01-04  208.067521
110     2019-01-05  176.668830
73  2019-01-06  311.626595
145     2019-01-07  283.241003
139     2019-01-08  284.652009

Date column is a date type. Here is my code :

#visualizations of time 
plt.figure(figsize=(10,8))
plt.title('Distribution of Time Feature')
sns.distplot(montant_df_pandas['dateTransmission'])

But I get this error :

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 2 plt.figure(figsize=(10,8)) 3 plt.title('Distribution of Time Feature') ----> 4 sns.distplot(montant_df_pandas['dateTransmission'])

~/anaconda3/envs/naboo-env/lib/python3.6/site-packages/seaborn/distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax) 196 line, = ax.plot(0, a.mean()) 197 else: --> 198 line, = ax.plot(a.mean(), 0) 199 color = line.get_color() 200 line.remove()

~/anaconda3/envs/naboo-env/lib/python3.6/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims) 73 is_float16_result = True 74 ---> 75 ret = umr_sum(arr, axis, dtype, out, keepdims) 76 if isinstance(ret, mu.ndarray): 77 ret = um.true_divide(

TypeError: ufunc add cannot use operands with types dtype('

Any idea to help me please?

Thanks

Upvotes: 0

Views: 222

Answers (1)

It seems like a date conversion issue between numpy and the other libraries link. I tried using a simple workaround for the problem. I am not sure about what was the actual plot we needed to generate but I gave it a shot(hope this helps).

from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(7), freq='D')

np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'Date': days, 'Amount': data})


df.set_index('Date')['Amount'].plot(legend=True)


Image of the output is attached here Image of the output is attached here

Upvotes: 2

Related Questions