Skip to content

Commit bc7ffdb

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 e88d8c0 commit bc7ffdb

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

pandas/tests/series/test_apply.py

Lines changed: 4 additions & 29 deletions
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
@@ -615,46 +615,21 @@ class DictWithoutMissing(dict):
615615
def test_map_abc_mapping(self):
616616
# https://github.com/pandas-dev/pandas/issues/29733
617617
# Check collections.abc.Mapping support as mapper for Series.map
618-
class NonDictMapping(abc.Mapping):
619-
def __init__(self):
620-
self._data = {3: "three"}
621-
622-
def __getitem__(self, key):
623-
return self._data.__getitem__(key)
624-
625-
def __iter__(self):
626-
return self._data.__iter__()
627-
628-
def __len__(self):
629-
return self._data.__len__()
630-
631618
s = Series([1, 2, 3])
632-
not_a_dictionary = NonDictMapping()
619+
not_a_dictionary = tm.TestNonDictMapping({3: "three"})
633620
result = s.map(not_a_dictionary)
634621
expected = Series([np.nan, np.nan, "three"])
635622
tm.assert_series_equal(result, expected)
636623

637624
def test_map_abc_mapping_with_missing(self):
638625
# https://github.com/pandas-dev/pandas/issues/29733
639626
# Check collections.abc.Mapping support as mapper for Series.map
640-
class NonDictMappingWithMissing(abc.Mapping):
641-
def __init__(self):
642-
self._data = {3: "three"}
643-
644-
def __getitem__(self, key):
645-
return self._data.__getitem__(key)
646-
647-
def __iter__(self):
648-
return self._data.__iter__()
649-
650-
def __len__(self):
651-
return self._data.__len__()
652-
627+
class NonDictMappingWithMissing(tm.TestNonDictMapping):
653628
def __missing__(self, key):
654629
return "missing"
655630

656631
s = Series([1, 2, 3])
657-
not_a_dictionary = NonDictMappingWithMissing()
632+
not_a_dictionary = NonDictMappingWithMissing({3: "three"})
658633
result = s.map(not_a_dictionary)
659634
# __missing__ is a dict concept, not a Mapping concept,
660635
# so it should not change the result!

pandas/tests/series/test_constructors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,14 @@ def create_data(constructor):
10811081
tm.assert_series_equal(result_datetime, expected)
10821082
tm.assert_series_equal(result_Timestamp, expected)
10831083

1084+
def test_constructor_mapping(self):
1085+
# GH 29788
1086+
ndm = tm.TestNonDictMapping({3: "three"})
1087+
result = Series(ndm)
1088+
expected = Series(["three"], index=[3])
1089+
1090+
tm.assert_series_equal(result, expected)
1091+
10841092
def test_constructor_list_of_tuples(self):
10851093
data = [(1, 1), (2, 2), (2, 3)]
10861094
s = Series(data)

pandas/util/testing.py

Lines changed: 15 additions & 1 deletion
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
@@ -2121,6 +2121,20 @@ def __init__(self, *args, **kwargs):
21212121
dict.__init__(self, *args, **kwargs)
21222122

21232123

2124+
class TestNonDictMapping(abc.Mapping):
2125+
def __init__(self, underlying_dict):
2126+
self._data = underlying_dict
2127+
2128+
def __getitem__(self, key):
2129+
return self._data.__getitem__(key)
2130+
2131+
def __iter__(self):
2132+
return self._data.__iter__()
2133+
2134+
def __len__(self):
2135+
return self._data.__len__()
2136+
2137+
21242138
def optional_args(decorator):
21252139
"""allows a decorator to take optional positional and keyword arguments.
21262140
Assumes that taking a single, callable, positional argument means that

0 commit comments

Comments
 (0)