Skip to content

BUG: pd.Series produces NaN for valid input as per documentation #34534

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

Closed
1 of 3 tasks
harishb00 opened this issue Jun 2, 2020 · 6 comments · Fixed by #39374
Closed
1 of 3 tasks

BUG: pd.Series produces NaN for valid input as per documentation #34534

harishb00 opened this issue Jun 2, 2020 · 6 comments · Fixed by #39374
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@harishb00
Copy link

harishb00 commented Jun 2, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample

import pandas as pd

dept = {'CS': 'Engineering',
        'ENG': 'Arts',
        'PHY': 'Science'}

dept = pd.Series(dept, index=['A', 'B', 'C'])
print(dept)

Problem description

pandas series is created by passing dict as input data and also index as additional argument. The indexes are updated but the values are changed to NaN. Whereas if I remove the index argument, it works fine.

Expected Output

CS Engineering
ENG Arts
PHY Science
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 69 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_India.1252

pandas : 0.25.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.11.1
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.2
matplotlib : 3.1.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.17
tables : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None

@harishb00 harishb00 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 2, 2020
@phofl
Copy link
Member

phofl commented Jun 2, 2020

Hi, thanks for your report.

Could you please assign a title for this issue?

What would you like to achieve with this statement? Your index ist ["A", "B", "C"]. You can assign values for these values, but not for the keys defined in your dictionary.
You can use either this

dept = {'CS': 'Engineering',
        'ENG': 'Arts',
        'PHY': 'Science'}

dept = pd.Series(dept, index=['CS', 'ENG', 'PHY'])

to get your output or

dept = {'A': 'Engineering',
        'B': 'Arts',
        'C': 'Science'}

dept = pd.Series(dept, index=['A', 'B', 'C'])

if your Index should be A,B,C

@harishb00 harishb00 changed the title BUG: BUG: pd.Series produces NaN for valid input as per documentation Jun 2, 2020
@harishb00
Copy link
Author

Below is the part of doucmentation.

indexarray-like or Index (1d)
Values must be hashable and have the same length as data. Non-unique index values are allowed. Will default to RangeIndex (0, 1, 2, …, n) if not provided. If both a dict and index sequence are used, the index will override the keys found in the dict.

Source: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.html#pandas-series

@phofl
Copy link
Member

phofl commented Jun 2, 2020

The index does override the keys found in the dict. It is not clear to me, whether the data should persist or not. I would assume, that it is tried to match the keys with the index values and fill the remaining keys with NaN. There exist tests which suggest this behavior.

Nevertheless, if I am not mistaken we could improve the documentation a bit to clarify the behavior.

@dsaxton
Copy link
Member

dsaxton commented Jun 3, 2020

I suppose instead of "override" the documentation could maybe be reworded to say that the Series will be reindexed based on the index argument. I'm not sure though why we would want to specify an index implicitly with dictionary input and then immediately provide a different index in the same call.

@dsaxton dsaxton added Docs Indexing Related to indexing on series/frames, not to indexes themselves and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 3, 2020
@jreback
Copy link
Contributor

jreback commented Jun 3, 2020

@TomAugspurger has an old PR about clarifying this behavior if you want see if can find the reference

there are also several issues about this (likely references from that PR)

@deepanshusadh
Copy link

@harry-b-harish, if you pass the index argument, it will surely take the indexes as passed in the argument.
The Series in pandas is defined for a one-dimensional labeled array capable of holding data and indexes.
Series in pandas is similar to a column in excel including indexes.
If you run the below code:

import pandas as pd

dept ={'CS': 'Engineering',
        'ENG': 'Arts',
        'PHY': 'Science'}

dept = pd.Series(dept)
print(dept)

The Output will be:

CS Engineering
ENG Arts
PHY Science
dtype: object

Pandas will automatically take the keys of the dictionary as indexes.

When you run the below code

import pandas as pd

dept =['Engineering',
        'Arts',
        'Science']

dept = pd.Series(dept)
dept

The output will be:

0 Engineering
1 Arts
2 Science
dtype: object

All you can see that pandas has taken default indexes on its own.

In your problem pandas Series is creating a Series including the indexes you passed as an argument but when it found that dept is a one-dimensional data with labels. It is like 2-dimensional for it as it has already created the index column(passed as an argument).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants