Skip to content

Commit fa83960

Browse files
ohad83jreback
authored andcommitted
TST - Moved NonDictMapping test util class to pandas/util/testing.py
TST - Added a test of constructing a series from NonDictMapping
1 parent d823d5d commit fa83960

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
@@ -630,46 +630,21 @@ class DictWithoutMissing(dict):
630630
def test_map_abc_mapping(self):
631631
# https://github.com/pandas-dev/pandas/issues/29733
632632
# Check collections.abc.Mapping support as mapper for Series.map
633-
class NonDictMapping(abc.Mapping):
634-
def __init__(self):
635-
self._data = {3: "three"}
636-
637-
def __getitem__(self, key):
638-
return self._data.__getitem__(key)
639-
640-
def __iter__(self):
641-
return self._data.__iter__()
642-
643-
def __len__(self):
644-
return self._data.__len__()
645-
646633
s = Series([1, 2, 3])
647-
not_a_dictionary = NonDictMapping()
634+
not_a_dictionary = tm.TestNonDictMapping({3: "three"})
648635
result = s.map(not_a_dictionary)
649636
expected = Series([np.nan, np.nan, "three"])
650637
tm.assert_series_equal(result, expected)
651638

652639
def test_map_abc_mapping_with_missing(self):
653640
# https://github.com/pandas-dev/pandas/issues/29733
654641
# Check collections.abc.Mapping support as mapper for Series.map
655-
class NonDictMappingWithMissing(abc.Mapping):
656-
def __init__(self):
657-
self._data = {3: "three"}
658-
659-
def __getitem__(self, key):
660-
return self._data.__getitem__(key)
661-
662-
def __iter__(self):
663-
return self._data.__iter__()
664-
665-
def __len__(self):
666-
return self._data.__len__()
667-
642+
class NonDictMappingWithMissing(tm.TestNonDictMapping):
668643
def __missing__(self, key):
669644
return "missing"
670645

671646
s = Series([1, 2, 3])
672-
not_a_dictionary = NonDictMappingWithMissing()
647+
not_a_dictionary = NonDictMappingWithMissing({3: "three"})
673648
result = s.map(not_a_dictionary)
674649
# __missing__ is a dict concept, not a Mapping concept,
675650
# 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
@@ -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

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
@@ -2128,6 +2128,20 @@ def __init__(self, *args, **kwargs):
21282128
dict.__init__(self, *args, **kwargs)
21292129

21302130

2131+
class TestNonDictMapping(abc.Mapping):
2132+
def __init__(self, underlying_dict):
2133+
self._data = underlying_dict
2134+
2135+
def __getitem__(self, key):
2136+
return self._data.__getitem__(key)
2137+
2138+
def __iter__(self):
2139+
return self._data.__iter__()
2140+
2141+
def __len__(self):
2142+
return self._data.__len__()
2143+
2144+
21312145
def optional_args(decorator):
21322146
"""allows a decorator to take optional positional and keyword arguments.
21332147
Assumes that taking a single, callable, positional argument means that

0 commit comments

Comments
 (0)