Skip to content

Unexpected results creating an empty Series #16737

Closed
@AllenDowney

Description

@AllenDowney

Code Sample, a copy-pastable example if possible

Python 3.5.1 |Anaconda custom (64-bit)| (default, Dec  7 2015, 11:16:01) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pandas import Series
>>> s1 = Series([])
>>> s1[0] = 100
>>> s1
0    100
dtype: int64
>>> s2 = Series()
>>> s2[0] = 100
Traceback (most recent call last):
  File "/home/downey/anaconda2/envs/py3k/lib/python3.5/site-packages/pandas/core/series.py", line 778, in _set_with_engine
    self.index._engine.set_value(values, key, value)
  File "pandas/_libs/index.pyx", line 116, in pandas._libs.index.IndexEngine.set_value (pandas/_libs/index.c:4649)
  File "pandas/_libs/index.pyx", line 124, in pandas._libs.index.IndexEngine.set_value (pandas/_libs/index.c:4475)
  File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085)
  File "pandas/_libs/hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20405)
  File "pandas/_libs/hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20359)
KeyError: 0

Problem description

When I create an empty series like this model = Series([]), I get the expected behavior when I try to set an element.

When I create an empty series like this model = Series(), I was hoping for the same behavior. It seems like a Series created with data=None isn't good for much. Or maybe there's a reason I would want one?

I realize that providing [] as an argument is not a big deal, but since I am using it as a teaching example, it would be nice to have one less thing to explain.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.1.final.0 python-bits: 64 OS: Linux OS-release: 3.19.0-32-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.utf8 LOCALE: en_US.UTF-8

pandas: 0.20.1
pytest: 2.8.5
pip: 8.1.1
setuptools: 20.3
Cython: 0.23.4
numpy: 1.11.0
scipy: 0.17.0
xarray: None
IPython: 4.1.2
sphinx: 1.3.5
patsy: 0.4.1
dateutil: 2.5.1
pytz: 2016.2
blosc: None
bottleneck: 1.2.1
tables: 3.3.0
numexpr: 2.5.2
feather: None
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.6.0
bs4: 4.4.1
html5lib: 0.999
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Labels

BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsIndexingRelated to indexing on series/frames, not to indexes themselves

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions