Skip to content

TST/REF: collect/parametrize tests from tests.generic #37730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 13, 2020
103 changes: 65 additions & 38 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from datetime import datetime

import numpy as np
import pytest
import pytz

from pandas.core.dtypes.common import (
is_categorical_dtype,
Expand All @@ -17,19 +19,16 @@
Timestamp,
cut,
date_range,
to_datetime,
)
import pandas._testing as tm


class TestDataFrameAlterAxes:
def test_convert_dti_to_series(self):
# don't cast a DatetimeIndex WITH a tz, leave as object
# GH 6032
idx = DatetimeIndex(
to_datetime(["2013-1-1 13:00", "2013-1-2 14:00"]), name="B"
).tz_localize("US/Pacific")
df = DataFrame(np.random.randn(2, 1), columns=["A"])
@pytest.fixture
def idx_expected(self):
idx = DatetimeIndex(["2013-1-1 13:00", "2013-1-2 14:00"], name="B").tz_localize(
"US/Pacific"
)

expected = Series(
np.array(
Expand All @@ -41,49 +40,76 @@ def test_convert_dti_to_series(self):
),
name="B",
)
assert expected.dtype == idx.dtype
Copy link
Member

Choose a reason for hiding this comment

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

I'm probably a little rusty on review :-) but is using assert in a fixture a common idiom?

Copy link
Member Author

Choose a reason for hiding this comment

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

its probably not super-encouraged but i dont think its an anti-pattern

return idx, expected

# convert index to series
result = Series(idx)
tm.assert_series_equal(result, expected)

# assign to frame
df["B"] = idx
result = df["B"]
tm.assert_series_equal(result, expected)

def test_to_series_keep_tz_deprecated_true(self, idx_expected):
# convert to series while keeping the timezone
idx, expected = idx_expected

msg = "stop passing 'keep_tz'"
with tm.assert_produces_warning(FutureWarning) as m:
result = idx.to_series(keep_tz=True, index=[0, 1])
assert msg in str(m[0].message)

tm.assert_series_equal(result, expected)

def test_to_series_keep_tz_deprecated_false(self, idx_expected):
idx, expected = idx_expected

with tm.assert_produces_warning(FutureWarning) as m:
result = idx.to_series(keep_tz=False, index=[0, 1])
tm.assert_series_equal(result, expected.dt.tz_convert(None))
msg = "do 'idx.tz_convert(None)' before calling"
assert msg in str(m[0].message)

def test_setitem_dt64series(self, idx_expected):
# convert to utc
idx, expected = idx_expected
df = DataFrame(np.random.randn(2, 1), columns=["A"])
df["B"] = idx

with tm.assert_produces_warning(FutureWarning) as m:
df["B"] = idx.to_series(keep_tz=False, index=[0, 1])
result = df["B"]
comp = Series(DatetimeIndex(expected.values).tz_localize(None), name="B")
tm.assert_series_equal(result, comp)
msg = "do 'idx.tz_convert(None)' before calling"
assert msg in str(m[0].message)

result = idx.to_series(index=[0, 1])
result = df["B"]
comp = Series(idx.tz_convert("UTC").tz_localize(None), name="B")
tm.assert_series_equal(result, comp)

def test_setitem_datetimeindex(self, idx_expected):
# setting a DataFrame column with a tzaware DTI retains the dtype
idx, expected = idx_expected
df = DataFrame(np.random.randn(2, 1), columns=["A"])

# assign to frame
df["B"] = idx
result = df["B"]
tm.assert_series_equal(result, expected)

with tm.assert_produces_warning(FutureWarning) as m:
result = idx.to_series(keep_tz=False, index=[0, 1])
tm.assert_series_equal(result, expected.dt.tz_convert(None))
msg = "do 'idx.tz_convert(None)' before calling"
assert msg in str(m[0].message)
def test_setitem_object_array_of_tzaware_datetimes(self, idx_expected):
# setting a DataFrame column with a tzaware DTI retains the dtype
idx, expected = idx_expected
df = DataFrame(np.random.randn(2, 1), columns=["A"])

# list of datetimes with a tz
# object array of datetimes with a tz
df["B"] = idx.to_pydatetime()
result = df["B"]
tm.assert_series_equal(result, expected)

def test_constructor_from_tzaware_datetimeindex(self, idx_expected):
# don't cast a DatetimeIndex WITH a tz, leave as object
# GH 6032
idx, expected = idx_expected

# convert index to series
result = Series(idx)
tm.assert_series_equal(result, expected)

def test_set_axis_setattr_index(self):
# GH 6785
# set the index manually
import pytz

df = DataFrame([{"ts": datetime(2014, 4, 1, tzinfo=pytz.utc), "foo": 1}])
expected = df.set_index("ts")
Expand All @@ -102,6 +128,7 @@ def test_dti_set_index_reindex(self):
df = df.reindex(idx2)
tm.assert_index_equal(df.index, idx2)

def test_dti_set_index_reindex_with_tz(self):
# GH 11314
# with tz
index = date_range(
Expand Down Expand Up @@ -130,16 +157,16 @@ class TestIntervalIndex:
def test_setitem(self):

df = DataFrame({"A": range(10)})
s = cut(df.A, 5)
assert isinstance(s.cat.categories, IntervalIndex)
ser = cut(df["A"], 5)
assert isinstance(ser.cat.categories, IntervalIndex)

# B & D end up as Categoricals
# the remainer are converted to in-line objects
# contining an IntervalIndex.values
df["B"] = s
df["C"] = np.array(s)
df["D"] = s.values
df["E"] = np.array(s.values)
df["B"] = ser
df["C"] = np.array(ser)
df["D"] = ser.values
df["E"] = np.array(ser.values)

assert is_categorical_dtype(df["B"].dtype)
assert is_interval_dtype(df["B"].cat.categories)
Expand All @@ -152,17 +179,17 @@ def test_setitem(self):
# they compare equal as Index
# when converted to numpy objects
c = lambda x: Index(np.array(x))
tm.assert_index_equal(c(df.B), c(df.B), check_names=False)
tm.assert_index_equal(c(df.B), c(df.B))
tm.assert_index_equal(c(df.B), c(df.C), check_names=False)
tm.assert_index_equal(c(df.B), c(df.D), check_names=False)
tm.assert_index_equal(c(df.B), c(df.D), check_names=False)
tm.assert_index_equal(c(df.C), c(df.D), check_names=False)

# B & D are the same Series
tm.assert_series_equal(df["B"], df["B"], check_names=False)
tm.assert_series_equal(df["B"], df["B"])
tm.assert_series_equal(df["B"], df["D"], check_names=False)

# C & E are the same Series
tm.assert_series_equal(df["C"], df["C"], check_names=False)
tm.assert_series_equal(df["C"], df["C"])
tm.assert_series_equal(df["C"], df["E"], check_names=False)

def test_set_reset_index(self):
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/frame/test_logical_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,42 @@
class TestDataFrameLogicalOperators:
# &, |, ^

@pytest.mark.parametrize(
"left, right, op, expected",
[
(
[True, False, np.nan],
[True, False, True],
operator.and_,
[True, False, False],
),
(
[True, False, True],
[True, False, np.nan],
operator.and_,
[True, False, False],
),
(
[True, False, np.nan],
[True, False, True],
operator.or_,
[True, False, False],
),
(
[True, False, True],
[True, False, np.nan],
operator.or_,
[True, False, True],
),
],
)
def test_logical_operators_nans(self, left, right, op, expected, frame_or_series):
# GH#13896
result = op(frame_or_series(left), frame_or_series(right))
expected = frame_or_series(expected)

tm.assert_equal(result, expected)

def test_logical_ops_empty_frame(self):
# GH#5808
# empty frames, non-mixed dtype
Expand Down
30 changes: 18 additions & 12 deletions pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import pandas._testing as tm


def check(result, expected=None):
if expected is not None:
tm.assert_frame_equal(result, expected)
result.dtypes
str(result)


class TestDataFrameNonuniqueIndexes:
def test_column_dups_operations(self):
def check(result, expected=None):
if expected is not None:
tm.assert_frame_equal(result, expected)
result.dtypes
str(result)

# assignment
# GH 3687
Expand Down Expand Up @@ -308,13 +310,7 @@ def test_column_dups2(self):
result = df.dropna(subset=["A", "C"], how="all")
tm.assert_frame_equal(result, expected)

def test_column_dups_indexing(self):
def check(result, expected=None):
if expected is not None:
tm.assert_frame_equal(result, expected)
result.dtypes
str(result)

def test_getitem_boolean_series_with_duplicate_columns(self):
# boolean indexing
# GH 4879
dups = ["A", "A", "C", "D"]
Expand All @@ -327,22 +323,32 @@ def check(result, expected=None):
result = df[df.C > 6]
check(result, expected)

def test_getitem_boolean_frame_with_duplicate_columns(self):
dups = ["A", "A", "C", "D"]

# where
df = DataFrame(
np.arange(12).reshape(3, 4), columns=["A", "B", "C", "D"], dtype="float64"
)
# `df > 6` is a DataFrame with the same shape+alignment as df
expected = df[df > 6]
expected.columns = dups
df = DataFrame(np.arange(12).reshape(3, 4), columns=dups, dtype="float64")
result = df[df > 6]
check(result, expected)

def test_getitem_boolean_frame_unaligned_with_duplicate_columns(self):
# `df.A > 6` is a DataFrame with a different shape from df
dups = ["A", "A", "C", "D"]

# boolean with the duplicate raises
df = DataFrame(np.arange(12).reshape(3, 4), columns=dups, dtype="float64")
msg = "cannot reindex from a duplicate axis"
with pytest.raises(ValueError, match=msg):
df[df.A > 6]

def test_column_dups_indexing(self):

# dup aligning operations should work
# GH 5185
df1 = DataFrame([1, 2, 3, 4, 5], index=[1, 2, 1, 2, 3])
Expand Down
69 changes: 35 additions & 34 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,22 @@ def test_transpose(self):
for df in [tm.makeTimeDataFrame()]:
tm.assert_frame_equal(df.transpose().transpose(), df)

def test_numpy_transpose(self):
msg = "the 'axes' parameter is not supported"
def test_numpy_transpose(self, frame_or_series):

s = tm.makeFloatSeries()
tm.assert_series_equal(np.transpose(s), s)
obj = tm.makeTimeDataFrame()
if frame_or_series is Series:
obj = obj["A"]

with pytest.raises(ValueError, match=msg):
np.transpose(s, axes=1)
if frame_or_series is Series:
# 1D -> np.transpose is no-op
tm.assert_series_equal(np.transpose(obj), obj)

df = tm.makeTimeDataFrame()
tm.assert_frame_equal(np.transpose(np.transpose(df)), df)
# round-trip preserved
tm.assert_equal(np.transpose(np.transpose(obj)), obj)

msg = "the 'axes' parameter is not supported"
with pytest.raises(ValueError, match=msg):
np.transpose(df, axes=1)
np.transpose(obj, axes=1)

def test_take(self):
indices = [1, 5, -2, 6, 3, -1]
Expand All @@ -415,23 +417,24 @@ def test_take(self):
)
tm.assert_frame_equal(out, expected)

def test_take_invalid_kwargs(self):
def test_take_invalid_kwargs(self, frame_or_series):
indices = [-3, 2, 0, 1]
s = tm.makeFloatSeries()
df = tm.makeTimeDataFrame()

for obj in (s, df):
msg = r"take\(\) got an unexpected keyword argument 'foo'"
with pytest.raises(TypeError, match=msg):
obj.take(indices, foo=2)
obj = tm.makeTimeDataFrame()
if frame_or_series is Series:
obj = obj["A"]

msg = r"take\(\) got an unexpected keyword argument 'foo'"
with pytest.raises(TypeError, match=msg):
obj.take(indices, foo=2)

msg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=msg):
obj.take(indices, out=indices)
msg = "the 'out' parameter is not supported"
with pytest.raises(ValueError, match=msg):
obj.take(indices, out=indices)

msg = "the 'mode' parameter is not supported"
with pytest.raises(ValueError, match=msg):
obj.take(indices, mode="clip")
msg = "the 'mode' parameter is not supported"
with pytest.raises(ValueError, match=msg):
obj.take(indices, mode="clip")

@pytest.mark.parametrize("is_copy", [True, False])
def test_depr_take_kwarg_is_copy(self, is_copy, frame_or_series):
Expand Down Expand Up @@ -473,21 +476,19 @@ def test_axis_numbers_deprecated(self, frame_or_series):
obj._AXIS_NUMBERS

def test_flags_identity(self, frame_or_series):
s = Series([1, 2])
obj = Series([1, 2])
if frame_or_series is DataFrame:
s = s.to_frame()
obj = obj.to_frame()

assert s.flags is s.flags
s2 = s.copy()
assert s2.flags is not s.flags
assert obj.flags is obj.flags
obj2 = obj.copy()
assert obj2.flags is not obj.flags

def test_slice_shift_deprecated(self):
def test_slice_shift_deprecated(self, frame_or_series):
# GH 37601
df = DataFrame({"A": [1, 2, 3, 4]})
s = Series([1, 2, 3, 4])

with tm.assert_produces_warning(FutureWarning):
df["A"].slice_shift()
obj = DataFrame({"A": [1, 2, 3, 4]})
if frame_or_series is DataFrame:
obj = obj["A"]

with tm.assert_produces_warning(FutureWarning):
s.slice_shift()
obj.slice_shift()
Loading