How to plot the daily maximum values

Viewed 363

I'm trying to plot the maximum value per day of a dataframe column (ext_temp):

import pandas as pd

data = {'vin': {0: 'VF1AG0000KF908155', 1: 'VF1AG0000KF908155', 2: 'VF1AG0000KF908155', 3: 'VF1AG0000KF908155', 4: 'VF1AG0000KF908155', 5: 'VF1AG0000KF908155', 6: 'VF1AG0000KF908155', 7: 'VF1AG0000KF908155', 8: 'VF1AG0000KF908155', 9: 'VF1AG0000KF908155'}, 'date': {0: pd.Timestamp('2019-09-27 07:07:02'), 1: pd.Timestamp('2019-09-27 09:23:08'), 2: pd.Timestamp('2019-09-27 09:39:08'), 3: pd.Timestamp('2020-07-15 11:46:41'), 4: pd.Timestamp('2020-07-16 07:17:52'), 5: pd.Timestamp('2020-07-16 09:23:47'), 6: pd.Timestamp('2020-09-11 07:43:05'), 7: pd.Timestamp('2020-09-17 15:00:33'), 8: pd.Timestamp('2020-10-21 06:49:58'), 9: pd.Timestamp('2020-10-21 14:47:33')}, 'sohe': {0: 101, 1: 101, 2: 101, 3: 96, 4: 96, 5: 96, 6: 96, 7: 96, 8: 96, 9: 96}, 'soc': {0: 60, 1: 63, 2: 99, 3: 66, 4: 68, 5: 69, 6: 86, 7: 58, 8: 9, 9: 9}, 'ext_temp': {0: 27, 1: 30, 2: 31, 3: 30, 4: 26, 5: 29, 6: 26, 7: 29, 8: 28, 9: 27}, 'battery_temp': {0: 27, 1: 33, 2: 32, 3: 26, 4: 26, 5: 26, 6: 26, 7: 30, 8: 27, 9: 29}}
df = pd.DataFrame(data)

Unfortunately, when trying to use

nd = "VF1AG0000KF908155"
df = charge[charge.vin==gop]

df = df.groupby(pd.Grouper(key = 'date', freq = 'D'))

fig,ax = plt.subplots()
ax.plot(df.date, df['ext_temp'].max())

I get the following error message : VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray

1 Answers
  • Using pd.Grouper has will fill missing days with a NaN
  • If you don't want missing days filled in, groupby the date component of 'date' by using the .dt extractor.
  • Use pandas.DataFrame.plot for plotting the dataframe
    • kind='bar' was used, since there's not much data. For a line plot, use kind='line'.

pd.Grouper

  • Note the need to use .dropna(), at least to plot the bar plot.
dfg = df.groupby(pd.Grouper(key='date', freq='D'))['ext_temp'].max().dropna()

ax = dfg.plot(kind='bar')

enter image description here

dfg = df.groupby(pd.Grouper(key='date', freq='D'))['ext_temp'].max().dropna()

ax = dfg.plot(kind='line')

enter image description here

.dt.date

  • Groupby only the date component of the 'date' column
dfg = df.groupby(df.date.dt.date)['ext_temp'].max()

ax = dfg.plot(kind='bar')

enter image description here

Related