Skip to content

Commit be05ebd

Browse files
committed
TST - Moved NonDictMapping test util class to pandas/util/testing.py
TST - Added a test of constructing a series from NonDictMapping
1 parent b59cb2d commit be05ebd

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

pandas/tests/series/test_apply.py

+4-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections import Counter, OrderedDict, abc, defaultdict
1+
from collections import Counter, OrderedDict, defaultdict
22
from itertools import chain
33

44
import numpy as np
@@ -612,46 +612,21 @@ class DictWithoutMissing(dict):
612612
def test_map_abc_mapping(self):
613613
# https://github.com/pandas-dev/pandas/issues/29733
614614
# Check collections.abc.Mapping support as mapper for Series.map
615-
class NonDictMapping(abc.Mapping):
616-
def __init__(self):
617-
self._data = {3: "three"}
618-
619-
def __getitem__(self, key):
620-
return self._data.__getitem__(key)
621-
622-
def __iter__(self):
623-
return self._data.__iter__()
624-
625-
def __len__(self):
626-
return self._data.__len__()
627-
628615
s = Series([1, 2, 3])
629-
not_a_dictionary = NonDictMapping()
616+
not_a_dictionary = tm.TestNonDictMapping({3: "three"})
630617
result = s.map(not_a_dictionary)
631618
expected = Series([np.nan, np.nan, "three"])
632619
tm.assert_series_equal(result, expected)
633620

634621
def test_map_abc_mapping_with_missing(self):
635622
# https://github.com/pandas-dev/pandas/issues/29733
636623
# Check collections.abc.Mapping support as mapper for Series.map
637-
class NonDictMappingWithMissing(abc.Mapping):
638-
def __init__(self):
639-
self._data = {3: "three"}
640-
641-
def __getitem__(self, key):
642-
return self._data.__getitem__(key)
643-
644-
def __iter__(self):
645-
return self._data.__iter__()
646-
647-
def __len__(self):
648-
return self._data.__len__()
649-
624+
class NonDictMappingWithMissing(tm.TestNonDictMapping):
650625
def __missing__(self, key):
651626
return "missing"
652627

653628
s = Series([1, 2, 3])
654-
not_a_dictionary = NonDictMappingWithMissing()
629+
not_a_dictionary = NonDictMappingWithMissing({3: "three"})
655630
result = s.map(not_a_dictionary)
656631
# __missing__ is a dict concept, not a Mapping concept,
657632
# so it should not change the result!

pandas/tests/series/test_constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,14 @@ def create_data(constructor):
10891089
tm.assert_series_equal(result_datetime, expected)
10901090
tm.assert_series_equal(result_Timestamp, expected)
10911091

1092+
def test_constructor_mapping(self):
1093+
# GH 29788
1094+
ndm = tm.TestNonDictMapping({3: "three"})
1095+
result = Series(ndm)
1096+
expected = Series(["three"], index=[3])
1097+
1098+
tm.assert_series_equal(result, expected)
1099+
10921100
def test_constructor_list_of_tuples(self):
10931101
data = [(1, 1), (2, 2), (2, 3)]
10941102
s = Series(data)

pandas/util/testing.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import bz2
2-
from collections import Counter
2+
from collections import Counter, abc
33
from contextlib import contextmanager
44
from datetime import datetime
55
from functools import wraps
@@ -2159,6 +2159,20 @@ def __init__(self, *args, **kwargs):
21592159
dict.__init__(self, *args, **kwargs)
21602160

21612161

2162+
class TestNonDictMapping(abc.Mapping):
2163+
def __init__(self, underlying_dict):
2164+
self._data = underlying_dict
2165+
2166+
def __getitem__(self, key):
2167+
return self._data.__getitem__(key)
2168+
2169+
def __iter__(self):
2170+
return self._data.__iter__()
2171+
2172+
def __len__(self):
2173+
return self._data.__len__()
2174+
2175+
21622176
def optional_args(decorator):
21632177
"""allows a decorator to take optional positional and keyword arguments.
21642178
Assumes that taking a single, callable, positional argument means that

0 commit comments

Comments
 (0)