Skip to content

Sync Fork from Upstream Repo #42

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 2 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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: 9 additions & 11 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,14 @@ def test_pickle(self, float_string_frame, timezone_frame):
def test_consolidate_datetime64(self):
# numpy vstack bug

data = """\
starting,ending,measure
2012-06-21 00:00,2012-06-23 07:00,77
2012-06-23 07:00,2012-06-23 16:30,65
2012-06-23 16:30,2012-06-25 08:00,77
2012-06-25 08:00,2012-06-26 12:00,0
2012-06-26 12:00,2012-06-27 08:00,77
"""
data = (
"starting,ending,measure\n"
"2012-06-21 00:00,2012-06-23 07:00,77\n"
"2012-06-23 07:00,2012-06-23 16:30,65\n"
"2012-06-23 16:30,2012-06-25 08:00,77\n"
"2012-06-25 08:00,2012-06-26 12:00,0\n"
"2012-06-26 12:00,2012-06-27 08:00,77\n"
)
df = pd.read_csv(StringIO(data), parse_dates=[0, 1])

ser_starting = df.starting
Expand All @@ -397,9 +397,6 @@ def test_is_mixed_type(self, float_frame, float_string_frame):
assert float_string_frame._is_mixed_type

def test_get_numeric_data(self):
# TODO(wesm): unused?
intname = np.dtype(np.int_).name # noqa
floatname = np.dtype(np.float_).name # noqa

datetime64name = np.dtype("M8[ns]").name
objectname = np.dtype(np.object_).name
Expand Down Expand Up @@ -581,6 +578,7 @@ def test_get_X_columns(self):
tm.assert_index_equal(df._get_numeric_data().columns, pd.Index(["a", "b", "e"]))

def test_strange_column_corruption_issue(self):
# FIXME: dont leave commented-out
# (wesm) Unclear how exactly this is related to internal matters
df = DataFrame(index=[0, 1])
df[0] = np.nan
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/frame/test_repr_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ def test_repr_unsortable(self, float_frame):
def test_repr_unicode(self):
uval = "\u03c3\u03c3\u03c3\u03c3"

# TODO(wesm): is this supposed to be used?
bval = uval.encode("utf-8") # noqa

df = DataFrame({"A": [uval, uval]})

result = repr(df)
Expand Down
73 changes: 64 additions & 9 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def finalize(self, other, method=None, **kwargs):

# reset
DataFrame._metadata = _metadata
DataFrame.__finalize__ = _finalize
DataFrame.__finalize__ = _finalize # FIXME: use monkeypatch

def test_set_attribute(self):
# Test for consistent setattr behavior when an attribute and a column
Expand All @@ -174,6 +174,69 @@ def test_set_attribute(self):
assert df.y == 5
tm.assert_series_equal(df["y"], Series([2, 4, 6], name="y"))

def test_deepcopy_empty(self):
# This test covers empty frame copying with non-empty column sets
# as reported in issue GH15370
empty_frame = DataFrame(data=[], index=[], columns=["A"])
empty_frame_copy = deepcopy(empty_frame)

self._compare(empty_frame_copy, empty_frame)


# formerly in Generic but only test DataFrame
class TestDataFrame2:
def test_validate_bool_args(self):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
invalid_values = [1, "True", [1, 2, 3], 5.0]

for value in invalid_values:
with pytest.raises(ValueError):
super(DataFrame, df).rename_axis(
mapper={"a": "x", "b": "y"}, axis=1, inplace=value
)

with pytest.raises(ValueError):
super(DataFrame, df).drop("a", axis=1, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df)._consolidate(inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).fillna(value=0, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).replace(to_replace=1, value=7, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).interpolate(inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df)._where(cond=df.a > 2, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).mask(cond=df.a > 2, inplace=value)

def test_unexpected_keyword(self):
# GH8597
df = DataFrame(np.random.randn(5, 2), columns=["jim", "joe"])
ca = pd.Categorical([0, 0, 2, 2, 3, np.nan])
ts = df["joe"].copy()
ts[2] = np.nan

with pytest.raises(TypeError, match="unexpected keyword"):
df.drop("joe", axis=1, in_place=True)

with pytest.raises(TypeError, match="unexpected keyword"):
df.reindex([1, 0], inplace=True)

with pytest.raises(TypeError, match="unexpected keyword"):
ca.fillna(0, inplace=True)

with pytest.raises(TypeError, match="unexpected keyword"):
ts.fillna(0, in_place=True)


class TestToXArray:
@pytest.mark.skipif(
not _XARRAY_INSTALLED
or _XARRAY_INSTALLED
Expand Down Expand Up @@ -272,11 +335,3 @@ def test_to_xarray(self):
expected["f"] = expected["f"].astype(object)
expected.columns.name = None
tm.assert_frame_equal(result, expected, check_index_type=False)

def test_deepcopy_empty(self):
# This test covers empty frame copying with non-empty column sets
# as reported in issue GH15370
empty_frame = DataFrame(data=[], index=[], columns=["A"])
empty_frame_copy = deepcopy(empty_frame)

self._compare(empty_frame_copy, empty_frame)
66 changes: 0 additions & 66 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,6 @@ def test_get_numeric_data(self):
# _get_numeric_data is includes _get_bool_data, so can't test for
# non-inclusion

def test_get_default(self):

# GH 7725
d0 = "a", "b", "c", "d"
d1 = np.arange(4, dtype="int64")
others = "e", 10

for data, index in ((d0, d1), (d1, d0)):
s = Series(data, index=index)
for i, d in zip(index, data):
assert s.get(i) == d
assert s.get(i, d) == d
assert s.get(i, "z") == d
for other in others:
assert s.get(other, "z") == "z"
assert s.get(other, other) == other

def test_nonzero(self):

# GH 4633
Expand Down Expand Up @@ -469,24 +452,6 @@ def test_split_compat(self):
assert len(np.array_split(o, 5)) == 5
assert len(np.array_split(o, 2)) == 2

def test_unexpected_keyword(self): # GH8597
df = DataFrame(np.random.randn(5, 2), columns=["jim", "joe"])
ca = pd.Categorical([0, 0, 2, 2, 3, np.nan])
ts = df["joe"].copy()
ts[2] = np.nan

with pytest.raises(TypeError, match="unexpected keyword"):
df.drop("joe", axis=1, in_place=True)

with pytest.raises(TypeError, match="unexpected keyword"):
df.reindex([1, 0], inplace=True)

with pytest.raises(TypeError, match="unexpected keyword"):
ca.fillna(0, inplace=True)

with pytest.raises(TypeError, match="unexpected keyword"):
ts.fillna(0, in_place=True)

# See gh-12301
def test_stat_unexpected_keyword(self):
obj = self._construct(5)
Expand Down Expand Up @@ -544,37 +509,6 @@ def test_truncate_out_of_bounds(self):
self._compare(big.truncate(before=0, after=3e6), big)
self._compare(big.truncate(before=-1, after=2e6), big)

def test_validate_bool_args(self):
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
invalid_values = [1, "True", [1, 2, 3], 5.0]

for value in invalid_values:
with pytest.raises(ValueError):
super(DataFrame, df).rename_axis(
mapper={"a": "x", "b": "y"}, axis=1, inplace=value
)

with pytest.raises(ValueError):
super(DataFrame, df).drop("a", axis=1, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df)._consolidate(inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).fillna(value=0, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).replace(to_replace=1, value=7, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).interpolate(inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df)._where(cond=df.a > 2, inplace=value)

with pytest.raises(ValueError):
super(DataFrame, df).mask(cond=df.a > 2, inplace=value)

def test_copy_and_deepcopy(self):
# GH 15444
for shape in [0, 1, 2]:
Expand Down
62 changes: 42 additions & 20 deletions pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,49 @@ def finalize(self, other, method=None, **kwargs):

# reset
Series._metadata = _metadata
Series.__finalize__ = _finalize
Series.__finalize__ = _finalize # FIXME: use monkeypatch

@pytest.mark.parametrize(
"s",
[
Series([np.arange(5)]),
pd.date_range("1/1/2011", periods=24, freq="H"),
pd.Series(range(5), index=pd.date_range("2017", periods=5)),
],
)
@pytest.mark.parametrize("shift_size", [0, 1, 2])
def test_shift_always_copy(self, s, shift_size):
# GH22397
assert s.shift(shift_size) is not s

@pytest.mark.parametrize("move_by_freq", [pd.Timedelta("1D"), pd.Timedelta("1M")])
def test_datetime_shift_always_copy(self, move_by_freq):
# GH22397
s = pd.Series(range(5), index=pd.date_range("2017", periods=5))
assert s.shift(freq=move_by_freq) is not s


class TestSeries2:
# moved from Generic
def test_get_default(self):

# GH#7725
d0 = ["a", "b", "c", "d"]
d1 = np.arange(4, dtype="int64")
others = ["e", 10]

for data, index in ((d0, d1), (d1, d0)):
s = Series(data, index=index)
for i, d in zip(index, data):
assert s.get(i) == d
assert s.get(i, d) == d
assert s.get(i, "z") == d
for other in others:
assert s.get(other, "z") == "z"
assert s.get(other, other) == other


class TestToXArray:
@pytest.mark.skipif(
not _XARRAY_INSTALLED
or _XARRAY_INSTALLED
Expand Down Expand Up @@ -242,22 +283,3 @@ def test_to_xarray(self):
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
assert isinstance(result, DataArray)
tm.assert_series_equal(result.to_series(), s)

@pytest.mark.parametrize(
"s",
[
Series([np.arange(5)]),
pd.date_range("1/1/2011", periods=24, freq="H"),
pd.Series(range(5), index=pd.date_range("2017", periods=5)),
],
)
@pytest.mark.parametrize("shift_size", [0, 1, 2])
def test_shift_always_copy(self, s, shift_size):
# GH22397
assert s.shift(shift_size) is not s

@pytest.mark.parametrize("move_by_freq", [pd.Timedelta("1D"), pd.Timedelta("1M")])
def test_datetime_shift_always_copy(self, move_by_freq):
# GH22397
s = pd.Series(range(5), index=pd.date_range("2017", periods=5))
assert s.shift(freq=move_by_freq) is not s
4 changes: 2 additions & 2 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_shift(self):

# GH8083 test the base class for shift
idx = self.create_index()
msg = "Not supported for type {}".format(type(idx).__name__)
msg = f"Not supported for type {type(idx).__name__}"
with pytest.raises(NotImplementedError, match=msg):
idx.shift(1)
with pytest.raises(NotImplementedError, match=msg):
Expand Down Expand Up @@ -808,7 +808,7 @@ def test_map_dictlike(self, mapper):

index = self.create_index()
if isinstance(index, (pd.CategoricalIndex, pd.IntervalIndex)):
pytest.skip("skipping tests for {}".format(type(index)))
pytest.skip(f"skipping tests for {type(index)}")

identity = mapper(index.values, index)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def test_str(self):
# test the string repr
idx = self.create_index()
idx.name = "foo"
assert not "length={}".format(len(idx)) in str(idx)
assert not (f"length={len(idx)}" in str(idx))
assert "'foo'" in str(idx)
assert type(idx).__name__ in str(idx)

if hasattr(idx, "tz"):
if idx.tz is not None:
assert idx.tz in str(idx)
if hasattr(idx, "freq"):
assert "freq='{idx.freqstr}'".format(idx=idx) in str(idx)
assert f"freq='{idx.freqstr}'" in str(idx)

def test_view(self):
i = self.create_index()
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def _mklbl(prefix, n):
return ["{prefix}{i}".format(prefix=prefix, i=i) for i in range(n)]
return [f"{prefix}{i}" for i in range(n)]


def _axify(obj, key, axis):
Expand Down Expand Up @@ -96,7 +96,7 @@ def setup_method(self, method):
for kind in self._kinds:
d = dict()
for typ in self._typs:
d[typ] = getattr(self, "{kind}_{typ}".format(kind=kind, typ=typ))
d[typ] = getattr(self, f"{kind}_{typ}")

setattr(self, kind, d)

Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ class TestReplaceSeriesCoercion(CoercionBase):

for tz in ["UTC", "US/Eastern"]:
# to test tz => different tz replacement
key = "datetime64[ns, {0}]".format(tz)
key = f"datetime64[ns, {tz}]"
rep[key] = [
pd.Timestamp("2011-01-01", tz=tz),
pd.Timestamp("2011-01-03", tz=tz),
Expand Down Expand Up @@ -1017,9 +1017,7 @@ def test_replace_series(self, how, to_key, from_key):
):

if compat.is_platform_32bit() or compat.is_platform_windows():
pytest.skip(
"32-bit platform buggy: {0} -> {1}".format(from_key, to_key)
)
pytest.skip(f"32-bit platform buggy: {from_key} -> {to_key}")

# Expected: do not downcast by replacement
exp = pd.Series(self.rep[to_key], index=index, name="yyy", dtype=from_key)
Expand Down
8 changes: 2 additions & 6 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ def test_iloc_getitem_bool(self):
def test_iloc_getitem_bool_diff_len(self, index):
# GH26658
s = Series([1, 2, 3])
msg = "Boolean index has wrong length: {} instead of {}".format(
len(index), len(s)
)
msg = f"Boolean index has wrong length: {len(index)} instead of {len(s)}"
with pytest.raises(IndexError, match=msg):
_ = s.iloc[index]

Expand Down Expand Up @@ -612,9 +610,7 @@ def test_iloc_mask(self):
r = expected.get(key)
if r != ans:
raise AssertionError(
"[{key}] does not match [{ans}], received [{r}]".format(
key=key, ans=ans, r=r
)
f"[{key}] does not match [{ans}], received [{r}]"
)

def test_iloc_non_unique_indexing(self):
Expand Down
Loading