Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
in that case. We *want* the dictionaries to be treated as scalars, so we
hack around pandas by using UserDicts.
"""
import collections
try:
from collections.abc import Iterable, Mapping, Sequence
except AttributeError:
from collections import Iterable, Mapping, Sequence
from collections import UserDict

import itertools
import numbers
import random
Expand All @@ -24,10 +29,10 @@


class JSONDtype(ExtensionDtype):
type = collections.Mapping
type = Mapping
name = 'json'
try:
na_value = collections.UserDict()
na_value = UserDict()
except AttributeError:
# source compatibility with Py2.
na_value = {}
Expand Down Expand Up @@ -64,14 +69,14 @@ def _from_sequence(cls, scalars):

@classmethod
def _from_factorized(cls, values, original):
return cls([collections.UserDict(x) for x in values if x != ()])
return cls([UserDict(x) for x in values if x != ()])

def __getitem__(self, item):
if isinstance(item, numbers.Integral):
return self.data[item]
elif isinstance(item, np.ndarray) and item.dtype == 'bool':
return self._from_sequence([x for x, m in zip(self, item) if m])
elif isinstance(item, collections.Iterable):
elif isinstance(item, Iterable):
# fancy indexing
return type(self)([self.data[i] for i in item])
else:
Expand All @@ -82,8 +87,7 @@ def __setitem__(self, key, value):
if isinstance(key, numbers.Integral):
self.data[key] = value
else:
if not isinstance(value, (type(self),
collections.Sequence)):
if not isinstance(value, (type(self), Sequence)):
# broadcast value
value = itertools.cycle([value])

Expand Down Expand Up @@ -174,6 +178,6 @@ def _values_for_argsort(self):

def make_data():
# TODO: Use a regular dict. See _NDFrameIndexer._setitem_with_indexer
return [collections.UserDict([
return [UserDict([
(random.choice(string.ascii_letters), random.randint(0, 100))
for _ in range(random.randint(0, 10))]) for _ in range(100)]
27 changes: 20 additions & 7 deletions pandas/tests/groupby/test_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@
'is_monotonic_decreasing',
])

def df_letters_fn():
letters = np.array(list(ascii_lowercase))
N = 10
random_letters = letters.take(np.random.randint(0, 26, N))
df = DataFrame({'floats': N / 10 * Series(np.random.random(N)),
'letters': Series(random_letters)})
return df

df_letters_var = df_letters_fn()





@pytest.fixture
def mframe():
Expand All @@ -114,16 +127,16 @@ def df():

@pytest.fixture
def df_letters():
letters = np.array(list(ascii_lowercase))
N = 10
random_letters = letters.take(np.random.randint(0, 26, N))
df = DataFrame({'floats': N / 10 * Series(np.random.random(N)),
'letters': Series(random_letters)})
return df
return df_letters_var()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can leave this like it was and feed df_letters into a new fixture



@pytest.fixture
def df_letters_floats():
return df_letters_var().floats


@pytest.mark.parametrize(
"obj, whitelist", zip((df_letters(), df_letters().floats),
"obj, whitelist", zip((df_letters, df_letters_floats),
(df_whitelist, s_whitelist)))
def test_groupby_whitelist(df_letters, obj, whitelist):
df = df_letters
Expand Down
26 changes: 21 additions & 5 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,9 @@ def test_normalize_date():
assert (result == datetime(2012, 9, 7))


the_year_1960 = Timestamp('1960-01-01')


@pytest.fixture(params=['D', 's', 'ms', 'us', 'ns'])
def units(request):
return request.param
Expand All @@ -1536,18 +1539,31 @@ def units(request):
@pytest.fixture
def epoch_1960():
# for origin as 1960-01-01
return Timestamp('1960-01-01')
return the_year_1960

@pytest.fixture
def epoch_1960_as_pydatetime():
# for origin as 1960-01-01
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give a doc-string to these (change the comment to a quoted string)

return the_year_1960.to_pydatetime()

@pytest.fixture
def epoch_1960_as_datetime64():
# for origin as 1960-01-01
return the_year_1960.to_datetime64()





@pytest.fixture
def units_from_epochs():
return list(range(5))


@pytest.fixture(params=[epoch_1960(),
epoch_1960().to_pydatetime(),
epoch_1960().to_datetime64(),
str(epoch_1960())])
@pytest.fixture(params=[epoch_1960,
epoch_1960_as_pydatetime,
epoch_1960_as_datetime64,
str(epoch_1960)])
def epochs(request):
return request.param

Expand Down
23 changes: 20 additions & 3 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,8 +1768,7 @@ def test_value_counts_categorical_not_ordered(self):
tm.assert_series_equal(idx.value_counts(normalize=True), exp)


@pytest.fixture
def s_main_dtypes():
def s_main_dtypes_var():
df = pd.DataFrame(
{'datetime': pd.to_datetime(['2003', '2002',
'2001', '2002',
Expand All @@ -1789,6 +1788,23 @@ def s_main_dtypes():
return df


@pytest.fixture
def s_main_dtypes():
df = s_main_dtypes_var()
return df

def s_main_dtypes_items():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can inject the fixture to subsequent fixtures

adf = s_main_dtypes_var()
# pytest.set_trace()
a_list = [v for k, v in adf.iteritems()]
return a_list


@pytest.fixture(name="s_main_dtypes_items")
def s_main_dtypes_iteritems():
a_list = s_main_dtypes_items
return a_list

class TestMode(object):

@pytest.mark.parametrize('dropna, expected', [
Expand Down Expand Up @@ -1992,9 +2008,10 @@ def test_error(self, r):
with tm.assert_raises_regex(TypeError, msg):
method(arg)

@pytest.fixture(name="s_main_dtypes_iteritems")
@pytest.mark.parametrize(
"s",
[v for k, v in s_main_dtypes().iteritems()])
s_main_dtypes_iteritems)
def test_nsmallest_nlargest(self, s):
# float, int, datetime64 (use i8), timedelts64 (same),
# object that are numbers, object that are strings
Expand Down