Skip to content
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

TST/REF: collect tests by method #37589

Merged
merged 7 commits into from
Nov 2, 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
47 changes: 47 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,53 @@ def test_setitem_extension_types(self, obj, dtype):

tm.assert_frame_equal(df, expected)

def test_setitem_dt64_ndarray_with_NaT_and_diff_time_units(self):
# GH#7492
data_ns = np.array([1, "nat"], dtype="datetime64[ns]")
result = Series(data_ns).to_frame()
result["new"] = data_ns
expected = DataFrame({0: [1, None], "new": [1, None]}, dtype="datetime64[ns]")
tm.assert_frame_equal(result, expected)

# OutOfBoundsDatetime error shouldn't occur
data_s = np.array([1, "nat"], dtype="datetime64[s]")
result["new"] = data_s
expected = DataFrame({0: [1, None], "new": [1e9, None]}, dtype="datetime64[ns]")
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("unit", ["h", "m", "s", "ms", "D", "M", "Y"])
def test_frame_setitem_datetime64_col_other_units(self, unit):
# Check that non-nano dt64 values get cast to dt64 on setitem
# into a not-yet-existing column
n = 100

dtype = np.dtype(f"M8[{unit}]")
vals = np.arange(n, dtype=np.int64).view(dtype)
ex_vals = vals.astype("datetime64[ns]")

df = DataFrame({"ints": np.arange(n)}, index=np.arange(n))
df[unit] = vals

assert df[unit].dtype == np.dtype("M8[ns]")
assert (df[unit].values == ex_vals).all()

@pytest.mark.parametrize("unit", ["h", "m", "s", "ms", "D", "M", "Y"])
def test_frame_setitem_existing_datetime64_col_other_units(self, unit):
# Check that non-nano dt64 values get cast to dt64 on setitem
# into an already-existing dt64 column
n = 100

dtype = np.dtype(f"M8[{unit}]")
vals = np.arange(n, dtype=np.int64).view(dtype)
ex_vals = vals.astype("datetime64[ns]")

df = DataFrame({"ints": np.arange(n)}, index=np.arange(n))
df["dates"] = np.arange(n, dtype=np.int64).view("M8[ns]")

# We overwrite existing dt64 column with new, non-nano dt64 vals
df["dates"] = vals
assert (df["dates"].values == ex_vals).all()

def test_setitem_dt64tz(self, timezone_frame):

df = timezone_frame
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def test_reset_index_tz(self, tz_aware_fixture):
expected["idx"] = expected["idx"].apply(lambda d: Timestamp(d, tz=tz))
tm.assert_frame_equal(df.reset_index(), expected)

@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
def test_frame_reset_index_tzaware_index(self, tz):
dr = date_range("2012-06-02", periods=10, tz=tz)
df = DataFrame(np.random.randn(len(dr)), dr)
roundtripped = df.reset_index().set_index("index")
xp = df.index.tz
rs = roundtripped.index.tz
assert xp == rs

def test_reset_index_with_intervals(self):
idx = IntervalIndex.from_breaks(np.arange(11), name="x")
original = DataFrame({"x": idx, "y": np.arange(10)})[["x", "y"]]
Expand Down
32 changes: 21 additions & 11 deletions pandas/tests/frame/methods/test_transpose.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,63 @@
import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, date_range
import pandas._testing as tm


class TestTranspose:
def test_transpose_tzaware_1col_single_tz(self):
# GH#26825
dti = pd.date_range("2016-04-05 04:30", periods=3, tz="UTC")
dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")

df = pd.DataFrame(dti)
df = DataFrame(dti)
assert (df.dtypes == dti.dtype).all()
res = df.T
assert (res.dtypes == dti.dtype).all()

def test_transpose_tzaware_2col_single_tz(self):
# GH#26825
dti = pd.date_range("2016-04-05 04:30", periods=3, tz="UTC")
dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")

df3 = pd.DataFrame({"A": dti, "B": dti})
df3 = DataFrame({"A": dti, "B": dti})
assert (df3.dtypes == dti.dtype).all()
res3 = df3.T
assert (res3.dtypes == dti.dtype).all()

def test_transpose_tzaware_2col_mixed_tz(self):
# GH#26825
dti = pd.date_range("2016-04-05 04:30", periods=3, tz="UTC")
dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")
dti2 = dti.tz_convert("US/Pacific")

df4 = pd.DataFrame({"A": dti, "B": dti2})
df4 = DataFrame({"A": dti, "B": dti2})
assert (df4.dtypes == [dti.dtype, dti2.dtype]).all()
assert (df4.T.dtypes == object).all()
tm.assert_frame_equal(df4.T.T, df4)

@pytest.mark.parametrize("tz", [None, "America/New_York"])
def test_transpose_preserves_dtindex_equality_with_dst(self, tz):
# GH#19970
idx = date_range("20161101", "20161130", freq="4H", tz=tz)
df = DataFrame({"a": range(len(idx)), "b": range(len(idx))}, index=idx)
result = df.T == df.T
expected = DataFrame(True, index=list("ab"), columns=idx)
tm.assert_frame_equal(result, expected)

def test_transpose_object_to_tzaware_mixed_tz(self):
# GH#26825
dti = pd.date_range("2016-04-05 04:30", periods=3, tz="UTC")
dti = date_range("2016-04-05 04:30", periods=3, tz="UTC")
dti2 = dti.tz_convert("US/Pacific")

# mixed all-tzaware dtypes
df2 = pd.DataFrame([dti, dti2])
df2 = DataFrame([dti, dti2])
assert (df2.dtypes == object).all()
res2 = df2.T
assert (res2.dtypes == [dti.dtype, dti2.dtype]).all()

def test_transpose_uint64(self, uint64_frame):

result = uint64_frame.T
expected = pd.DataFrame(uint64_frame.values.T)
expected = DataFrame(uint64_frame.values.T)
expected.index = ["A", "B"]
tm.assert_frame_equal(result, expected)

Expand All @@ -63,7 +73,7 @@ def test_transpose_float(self, float_frame):

# mixed type
index, data = tm.getMixedTypeDict()
mixed = pd.DataFrame(data, index=index)
mixed = DataFrame(data, index=index)

mixed_T = mixed.T
for col, s in mixed_T.items():
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2737,6 +2737,35 @@ def test_constructor_list_str_na(self, string_dtype):


class TestDataFrameConstructorWithDatetimeTZ:
@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
def test_construction_preserves_tzaware_dtypes(self, tz):
# after GH#7822
# these retain the timezones on dict construction
dr = date_range("2011/1/1", "2012/1/1", freq="W-FRI")
dr_tz = dr.tz_localize(tz)
df = DataFrame({"A": "foo", "B": dr_tz}, index=dr)
tz_expected = DatetimeTZDtype("ns", dr_tz.tzinfo)
assert df["B"].dtype == tz_expected

# GH#2810 (with timezones)
datetimes_naive = [ts.to_pydatetime() for ts in dr]
datetimes_with_tz = [ts.to_pydatetime() for ts in dr_tz]
df = DataFrame({"dr": dr})
df["dr_tz"] = dr_tz
df["datetimes_naive"] = datetimes_naive
df["datetimes_with_tz"] = datetimes_with_tz
result = df.dtypes
expected = Series(
[
np.dtype("datetime64[ns]"),
DatetimeTZDtype(tz=tz),
np.dtype("datetime64[ns]"),
DatetimeTZDtype(tz=tz),
],
index=["dr", "dr_tz", "datetimes_naive", "datetimes_with_tz"],
)
tm.assert_series_equal(result, expected)

def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
# GH#25843
tz = tz_aware_fixture
Expand Down
23 changes: 0 additions & 23 deletions pandas/tests/frame/test_nonunique_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,29 +400,6 @@ def check(result, expected=None):
result = z.loc[["a", "c", "a"]]
check(result, expected)

def test_column_dups_indexing2(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

where did this go?

Copy link
Member Author

Choose a reason for hiding this comment

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

test_arithmetic_with_duplicate_index


# GH 8363
# datetime ops with a non-unique index
df = DataFrame(
{"A": np.arange(5, dtype="int64"), "B": np.arange(1, 6, dtype="int64")},
index=[2, 2, 3, 3, 4],
)
result = df.B - df.A
expected = Series(1, index=[2, 2, 3, 3, 4])
tm.assert_series_equal(result, expected)

df = DataFrame(
{
"A": date_range("20130101", periods=5),
"B": date_range("20130101 09:00:00", periods=5),
},
index=[2, 2, 3, 3, 4],
)
result = df.B - df.A
expected = Series(pd.Timedelta("9 hours"), index=[2, 2, 3, 3, 4])
tm.assert_series_equal(result, expected)

def test_columns_with_dups(self):
# GH 3468 related

Expand Down
57 changes: 0 additions & 57 deletions pandas/tests/frame/test_timeseries.py

This file was deleted.

60 changes: 0 additions & 60 deletions pandas/tests/frame/test_timezones.py

This file was deleted.

15 changes: 15 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@ def test_getitem_boolean_different_order(self, string_series):
exp = string_series[string_series > 0]
tm.assert_series_equal(sel, exp)

def test_getitem_boolean_contiguous_preserve_freq(self):
rng = date_range("1/1/2000", "3/1/2000", freq="B")

mask = np.zeros(len(rng), dtype=bool)
mask[10:20] = True

masked = rng[mask]
expected = rng[10:20]
assert expected.freq == rng.freq
tm.assert_index_equal(masked, expected)

mask[22] = True
masked = rng[mask]
assert masked.freq is None


class TestGetitemCallable:
def test_getitem_callable(self):
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/series/methods/test_is_monotonic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import numpy as np

from pandas import Series, date_range


class TestIsMonotonic:
def test_is_monotonic_numeric(self):

ser = Series(np.random.randint(0, 10, size=1000))
assert not ser.is_monotonic
ser = Series(np.arange(1000))
assert ser.is_monotonic is True
assert ser.is_monotonic_increasing is True
ser = Series(np.arange(1000, 0, -1))
assert ser.is_monotonic_decreasing is True

def test_is_monotonic_dt64(self):

ser = Series(date_range("20130101", periods=10))
assert ser.is_monotonic is True
assert ser.is_monotonic_increasing is True

ser = Series(list(reversed(ser)))
assert ser.is_monotonic is False
assert ser.is_monotonic_decreasing is True
Loading