Skip to content

Commit 9dbf262

Browse files
committed
fixed requested changes
1 parent 18ef527 commit 9dbf262

File tree

5 files changed

+52
-39
lines changed

5 files changed

+52
-39
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ MultiIndex
699699
- :meth:`MultiIndex.insert` would not insert NA value correctly at unified location of index -1 (:issue:`59003`)
700700
- :func:`MultiIndex.get_level_values` accessing a :class:`DatetimeIndex` does not carry the frequency attribute along (:issue:`58327`, :issue:`57949`)
701701
- Bug in :class:`DataFrame` arithmetic operations in case of unaligned MultiIndex columns (:issue:`60498`)
702-
- Bug in :meth:`MultiIndex.from_tuples` causing wrong output with input of type tuples having NaN values (:issue:`60695`)
702+
- Bug in :meth:`MultiIndex.from_tuples` causing wrong output with input of type tuples having NaN values (:issue:`60695`, :issue:`60988`)
703703

704704
I/O
705705
^^^

pandas/core/algorithms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,8 @@ def map_array(
16471647
If the function returns a tuple with more than one element
16481648
a MultiIndex will be returned.
16491649
"""
1650+
from pandas import Index
1651+
16501652
if na_action not in (None, "ignore"):
16511653
msg = f"na_action must either be 'ignore' or None, {na_action} was passed"
16521654
raise ValueError(msg)
@@ -1676,6 +1678,10 @@ def map_array(
16761678

16771679
if len(mapper) == 0:
16781680
mapper = Series(mapper, dtype=np.float64)
1681+
elif isinstance(mapper, dict):
1682+
mapper = Series(
1683+
mapper.values(), index=Index(mapper.keys(), tupleize_cols=False)
1684+
)
16791685
else:
16801686
mapper = Series(mapper)
16811687

pandas/tests/indexes/multi/test_constructors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def test_from_tuples_with_tuple_label():
412412

413413
@pytest.mark.parametrize(
414414
"keys, expected",
415-
(
415+
[
416416
((("l1",), ("l1", "l2")), (("l1", np.nan), ("l1", "l2"))),
417417
(
418418
(
@@ -424,7 +424,7 @@ def test_from_tuples_with_tuple_label():
424424
),
425425
(("l1", "l2"), ("l1", np.nan)),
426426
),
427-
),
427+
],
428428
)
429429
def test_from_tuples_with_various_tuple_lengths(keys, expected):
430430
# GH 60695

pandas/tests/series/methods/test_map.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,7 @@ def test_map_dict_with_tuple_keys():
288288
df = DataFrame({"a": [(1,), (2,), (3, 4), (5, 6)]})
289289
label_mappings = {(1,): "A", (2,): "B", (3, 4): "A", (5, 6): "B"}
290290

291-
# GH 60695
292-
df["labels"] = df["a"].apply(lambda x: label_mappings.get(x, None))
293-
291+
df["labels"] = df["a"].map(label_mappings)
294292
df["expected_labels"] = Series(["A", "B", "A", "B"], index=df.index)
295293
# All labels should be filled now
296294
tm.assert_series_equal(df["labels"], df["expected_labels"], check_names=False)

pandas/tests/series/test_constructors.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,16 +1441,17 @@ def test_constructor_tuple_of_tuples(self):
14411441
s = Series(data)
14421442
assert tuple(s) == data
14431443

1444-
def test_constructor_dict_of_tuples(self):
1445-
data = {(1, 2): 3, (None, 5): 6}
1446-
result = Series(data).sort_values()
1447-
expected = Series([3, 6], index=MultiIndex.from_tuples([(1, 2), (None, 5)]))
1448-
tm.assert_series_equal(result, expected)
1449-
1450-
# GH 60695
1451-
data = {(1,): 3, (4, 5): 6}
1444+
@pytest.mark.parametrize(
1445+
"data, expected_values, expected_index",
1446+
[
1447+
({(1, 2): 3, (None, 5): 6}, [3, 6], [(1, 2), (None, 5)]),
1448+
# GH 60695 test case
1449+
({(1,): 3, (4, 5): 6}, [3, 6], [(1, None), (4, 5)]),
1450+
],
1451+
)
1452+
def test_constructor_dict_of_tuples(self, data, expected_values, expected_index):
14521453
result = Series(data).sort_values()
1453-
expected = Series([3, 6], index=MultiIndex.from_tuples([(1, None), (4, 5)]))
1454+
expected = Series(expected_values, index=MultiIndex.from_tuples(expected_index))
14541455
tm.assert_series_equal(result, expected)
14551456

14561457
# https://github.com/pandas-dev/pandas/issues/22698
@@ -1866,32 +1867,40 @@ class A(OrderedDict):
18661867
series = Series(A(data))
18671868
tm.assert_series_equal(series, expected)
18681869

1869-
def test_constructor_dict_multiindex(self):
1870-
d = {("a", "a"): 0.0, ("b", "a"): 1.0, ("b", "c"): 2.0}
1871-
_d = sorted(d.items())
1872-
result = Series(d)
1873-
expected = Series(
1874-
[x[1] for x in _d], index=MultiIndex.from_tuples([x[0] for x in _d])
1875-
)
1876-
tm.assert_series_equal(result, expected)
1870+
@pytest.mark.parametrize(
1871+
"data, expected_index_multi, expected_index_single",
1872+
[
1873+
(
1874+
{("a", "a"): 0.0, ("b", "a"): 1.0, ("b", "c"): 2.0},
1875+
MultiIndex.from_tuples([("a", "a"), ("b", "a"), ("b", "c")]),
1876+
None,
1877+
),
1878+
(
1879+
{("a",): 0.0, ("a", "b"): 1.0},
1880+
MultiIndex.from_tuples([("a",), ("a", "b")]),
1881+
None,
1882+
),
1883+
(
1884+
{("a", "a"): 0.0, ("b", "a"): 1.0, ("b", "c"): 2.0, "z": 111.0},
1885+
None,
1886+
Index(["z", ("a", "a"), ("b", "a"), ("b", "c")], tupleize_cols=False),
1887+
),
1888+
],
1889+
)
1890+
def test_constructor_dict_multiindex(
1891+
data, expected_index_multi, expected_index_single
1892+
):
1893+
_d = sorted(data.items())
18771894

1878-
d["z"] = 111.0
1879-
_d.insert(0, ("z", d["z"]))
1880-
result = Series(d)
1881-
expected = Series(
1882-
[x[1] for x in _d], index=Index([x[0] for x in _d], tupleize_cols=False)
1883-
)
1884-
result = result.reindex(index=expected.index)
1885-
tm.assert_series_equal(result, expected)
1895+
result = Series(data)
1896+
if expected_index_multi:
1897+
expected = Series([x[1] for x in _d], index=expected_index_multi)
1898+
tm.assert_series_equal(result, expected)
18861899

1887-
# GH 60695
1888-
d = {("a",): 0.0, ("a", "b"): 1.0}
1889-
_d = sorted(d.items())
1890-
result = Series(d)
1891-
expected = Series(
1892-
[x[1] for x in _d], index=MultiIndex.from_tuples([x[0] for x in _d])
1893-
)
1894-
tm.assert_series_equal(result, expected)
1900+
if expected_index_single:
1901+
result = result.reindex(index=expected_index_single)
1902+
expected = Series([x[1] for x in _d], index=expected_index_single)
1903+
tm.assert_series_equal(result, expected)
18951904

18961905
def test_constructor_dict_multiindex_reindex_flat(self):
18971906
# construction involves reindexing with a MultiIndex corner case

0 commit comments

Comments
 (0)