ValueError on Python: I don't know what to do with Length mismatch.

Asked 2 years ago, Updated 2 years ago, 81 views

The code below suddenly displays ValueError: Length mismatch, and I don't know how to deal with it.I thought it was because I got the same securities code over and over again, but it was working fine until yesterday, so I don't know where to improve it.
Could you tell me the solution?

%matplotlib inline
import pandas aspd
import matplotlib.pyplot asplt
import seaborn as sns
from pandas_datareader.stooq import StooqDailyReader
from datetime import datetime
import numpy as np
import sys
import matplotlib.dates asmdate


a = [0]
codes = [1301, 1332, 1333, 1352 ]

codes = sorted (codes)
start = datetime (2021, 4, 1)
end = datetime (2021, 6, 22)

dfc=(
       StooqDailyReader ([f'{n}.JP'for n in codes], start, end).read()
       .Close.iloc[::-1].reset_index(drop=True)
       .set_axis([codes]*len(a),axis=1)
     )

print(dfc)

print(dfc.corr())
plt.figure(figsize=(20,20))
sns.heatmap(dfc.corr(),not=True,vmax=1,vmin=-1,cmap='coolwarm',center=0)

The following error details


ValueError Traceback (most recent call last)
<ipython-input-33-4bb2b2d041c0>in<module>()
     20 StooqDailyReader ([f'{n}.JP'for n in codes], start, end).read()
     21.Close.iloc[::-1].reset_index(drop=True)
--- > 22.set_axis ([codes])*len(a),axis=1)
     23      )
     24 

6 frames
pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__set_()

/usr/local/lib/python 3.7/dist-packages/pandas/core/internals/managers.py in set_axis(self,axis,new_labels)
    225 if new_len!=old_len:
    226 raise ValueError(
-->227f "Length mismatch: Expected axis has {old_len} elements, new"
    228f "values have {new_len} elements"
    229             )

ValueError: Length mismatch: Expected axis has 2 elements, new values have 3 elements

Thank you for your cooperation.

python pandas numpy matplotlib

2022-09-29 21:46

1 Answers

Duplicate securities codes (for example, codes=[1301,1301,1333,1352]) will result in fewer columns of data frames because data from the same securities code is retrieved only once.

As a possible situation, codes may contain a securities code that does not exist.For example, codes=[1301,1302,1332] results in a similar error (1302.JP does not exist).

I have rewritten it considering that there is a duplicate or non-existent securities code.

import pandas as pd
import re
from pandas_datareader.stooq import StooqDailyReader
from datetime import datetime

codes = [1301,1332,1333]
## codes = [1301,1301,1332,1333] # Duplicate
## codes = [1301,1302,1332,1333] # Missing number

codes = sorted (codes)
start = datetime (2021, 6, 1)
end = datetime (2021, 6, 22)

dfc=(
  StooqDailyReader ([f'{n}.JP'for n in codes], start, end).read()
  .Close.astype(int).sort_index()
  .rename_axis (index=None, columns=None)
  .rename(columns=lambdac:re.sub(r'\.JP$', '', c))
)
  • Duplicate
#1301.JP Duplicate
codes = [1301,1301,1332,1333]

print(dfc.head())

            1301  1332  1333
2021-06-01  2974   514  2435
2021-06-02  2918   516  2435
2021-06-03  2936   534  2465
2021-06-04  2935   540  2455
2021-06-07  2942   548  2448
  • missing number
#1302.JP does not exist
codes = [1301,1302,1332,1333]

print(dfc.head())

            1301  1332  1333
2021-06-01  2974   514  2435
2021-06-02  2918   516  2435
2021-06-03  2936   534  2465
2021-06-04  2935   540  2455
2021-06-07  2942   548  2448


2022-09-29 21:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.