Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: User-facing AssertionError with add column to SparseDataFrame #25484

Closed
simonjayhawkins opened this issue Feb 28, 2019 · 1 comment

Comments

@simonjayhawkins
Copy link
Member

commented Feb 28, 2019

see also #5289

Code Sample, a copy-pastable example if possible

import numpy as np
import pandas as pd
data = {'A': [np.nan, np.nan, np.nan, 0, 1, 2, 3, 4, 5, 6],
        'B': [0, 1, 2, np.nan, np.nan, np.nan, 3, 4, 5, 6],
        'C': np.arange(10, dtype=np.float64),
        'D': [0, 1, 2, 3, 4, 5, np.nan, np.nan, np.nan, np.nan]}
dates = pd.bdate_range('1/1/2011', periods=10)
float_frame = pd.SparseDataFrame(data, index=dates, default_kind='block')
N = len(float_frame)
float_frame['foo'] = np.random.randn(N - 1)

Problem description

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-8-0a4f051f0d7a> in <module>
      8 float_frame = pd.SparseDataFrame(data, index=dates, default_kind='block')
      9 N = len(float_frame)
---> 10 float_frame['foo'] = np.random.randn(N - 1)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
   3117         else:
   3118             # set column
-> 3119             self._set_item(key, value)
   3120 
   3121     def _setitem_slice(self, key, value):

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
   3192 
   3193         self._ensure_valid_index(value)
-> 3194         value = self._sanitize_column(key, value)
   3195         NDFrame._set_item(self, key, value)
   3196 

~\Anaconda3\lib\site-packages\pandas\core\sparse\frame.py in _sanitize_column(self, key, value, **kwargs)
    418             else:
    419                 if len(value) != len(self.index):
--> 420                     raise AssertionError('Length of values does not match '
    421                                          'length of index')
    422                 clean = sp_maker(value)

AssertionError: Length of values does not match length of index

Expected Output

ValueError: Length of values does not match length of index

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.4
pytest: 4.0.2
pip: 18.1
setuptools: 40.6.3
Cython: 0.29.2
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@gfyoung

This comment has been minimized.

Copy link
Member

commented Mar 1, 2019

That makes sense. SparseDataFrame is getting deprecated, but in the mean time, let's keep it user-friendly until it gets removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.