0

I am new to Python and try to improve my experience by editing projects. I have an error that occurs when I try to compile the code below:

import matplotlib.ticker as mtick

cities = ['Los Angeles', 'Florida', 'Texas']
fig, axes = plt.subplots(3, 1, figsize=(10, 11), sharex=True)
monthly_all = {}
 
for index in range(3):
    region = regions[index]
    data = result_all[region]
    city = cities[index] # 3 cities (NY, FL and TX)
    
    verif = data.truncate(before='2020-01-01')
 
    axes[index].plot(verif.index, verif.y, color=default_colors[1], linestyle='--', lw=2, label='Ground truth')
    train = verif[verif['train']]
    axes[index].plot(train.index, train.yhat, color=default_colors[0], lw=2, label='Prediction before Lockdown')
    test = verif[verif['train'] == False]
    axes[index].plot(test.index, test.yhat, color=default_colors[2], lw=2, label='Prediction after Lockdown')
    axes[index].axvline(verif[verif['train']].index[-1], color='black', alpha=0.5, label = 'Lockdown')
 
    axes[index].set_ylabel(f'{city}\nDaily Electricity Use [GWh]')
    axes[index].grid(ls=':', lw=0.5)
    
    # summary of monthly preduction
    verif['month'] = verif.index.month
    monthly = verif.groupby('month').sum()
    monthly['percent'] = monthly['y']/monthly['yhat']
    monthly_all[region] = monthly.percent.values
 
axes[2].legend(loc='center left', bbox_to_anchor=(1,1.65))
axes[2].set_xlabel('Time')
 
plt.savefig(generate_fig_path('Figure 18'))
 
monthly_all_df = pd.DataFrame(monthly_all)-1
monthly_all_df.columns = cities
monthly_covid = monthly_all_df.iloc[2:,:]
monthly_covid.index = ['Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug', 'Sep.']
 
ax = monthly_covid.plot.bar(rot=0)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
ax.yaxis.set_major_formatter(mtick.PercentFormatter(1.0))
ax.set_ylabel('Electricity Use Change \nSince COVID-19 Lockdown')
plt.savefig(generate_fig_path('Figure 19'))

The error code:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-89-0ea7229ccde0> in <module>
----> 1 monthly_all_df = pd.DataFrame(monthly_all)-1
      2 monthly_all_df.columns = cities
      3 monthly_covid = monthly_all_df.iloc[2:,:]
      4 monthly_covid.index = ['Mar.', 'Apr.', 'May', 'Jun.', 'Jul.', 'Aug', 'Sep.']
      5 
 
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
    466 
    467         elif isinstance(data, dict):
--> 468             mgr = init_dict(data, index, columns, dtype=dtype)
    469         elif isinstance(data, ma.MaskedArray):
    470             import numpy.ma.mrecords as mrecords
 
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\construction.py in init_dict(data, index, columns, dtype)
    281             arr if not is_datetime64tz_dtype(arr) else arr.copy() for arr in arrays
    282         ]
--> 283     return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
    284 
    285 
 
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype, verify_integrity)
     76         # figure out the index, if necessary
     77         if index is None:
---> 78             index = extract_index(arrays)
     79         else:
     80             index = ensure_index(index)
 
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\internals\construction.py in extract_index(data)
    395             lengths = list(set(raw_lengths))
    396             if len(lengths) > 1:
--> 397                 raise ValueError("arrays must all be same length")
    398 
    399             if have_dicts:
 
ValueError: arrays must all be same length

I also googled about the error message and try to apply it to my code but I stacked it without any solution. Canyou figure out what I am missing here?

medo0070
  • 511
  • 1
  • 5
  • 21
  • 1
    Just like is says, all the arrays in `monthly_all`, are not the same length. Please see [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard(sep=',')`](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. – Trenton McKinney Aug 26 '21 at 02:59
  • Thanks for your kind information. I will read them and edit the post. – medo0070 Aug 26 '21 at 11:52

0 Answers0