Skip to content

Commit 3afd05c

Browse files
committed
BUG: fix initialization of Series with dict containing NaN key
closes pandas-dev#18480
1 parent 38f41e6 commit 3afd05c

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

doc/source/whatsnew/v0.22.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,5 @@ Other
205205

206206
- Improved error message when attempting to use a Python keyword as an identifier in a numexpr query (:issue:`18221`)
207207
- Fixed a bug where creating a Series from an array that contains both tz-naive and tz-aware values will result in a Series whose dtype is tz-aware instead of object (:issue:`16406`)
208+
- Fixed construction of ``Series`` from ``dict`` containing ``NaN`` as key (:issue:`18480`)
208209
-

pandas/core/series.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
_default_index,
4343
_asarray_tuplesafe,
4444
_values_from_object,
45-
_try_sort,
4645
_maybe_match_name,
4746
SettingWithCopyError,
4847
_maybe_box_datetimelike,
@@ -198,18 +197,26 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
198197
data = data.reindex(index, copy=copy)
199198
data = data._data
200199
elif isinstance(data, dict):
201-
if index is None:
202-
if isinstance(data, OrderedDict):
203-
index = Index(data)
204-
else:
205-
index = Index(_try_sort(data))
206-
207-
try:
208-
data = index._get_values_from_dict(data)
209-
except TypeError:
210-
data = ([data.get(i, np.nan) for i in index]
211-
if data else np.nan)
212-
200+
if data:
201+
keys, values = zip(*compat.iteritems(data))
202+
else:
203+
if index is None:
204+
index = Index([])
205+
keys = index
206+
values = np.array([np.nan] * len(index))
207+
s = Series(values, index=keys, dtype=dtype)
208+
if index is not None:
209+
if not index.equals(s.index):
210+
s = s.reindex(index)
211+
elif not isinstance(data, OrderedDict):
212+
try:
213+
s = s.sort_index()
214+
except TypeError:
215+
pass
216+
data = s._data
217+
index = s.index
218+
copy = False
219+
dtype = None
213220
elif isinstance(data, SingleBlockManager):
214221
if index is None:
215222
index = data.index

pandas/tests/series/test_constructors.py

+13
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,19 @@ def test_constructor_dict(self):
625625
expected.iloc[1] = 1
626626
assert_series_equal(result, expected)
627627

628+
def test_constructor_dict_nan_key(self):
629+
# GH 18480
630+
d = {1: 'a', 2: 'b', np.nan: 'c'}
631+
result = Series(d).sort_index()
632+
expected = Series(['a', 'b', 'c'], index=[1, 2, np.nan])
633+
assert_series_equal(result, expected)
634+
635+
# Different NaNs:
636+
d = {1: 'a', 2: 'b', float('nan'): 'c', float('nan'): 'd'}
637+
result = Series(d).sort_values()
638+
expected = Series(['a', 'b', 'c', 'd'], index=[1, 2, np.nan, np.nan])
639+
assert_series_equal(result, expected)
640+
628641
def test_constructor_dict_datetime64_index(self):
629642
# GH 9456
630643

0 commit comments

Comments
 (0)