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 #37449

Merged
merged 8 commits into from
Oct 29, 2020
38 changes: 37 additions & 1 deletion pandas/tests/base/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pandas.core.dtypes.dtypes import DatetimeTZDtype

import pandas as pd
from pandas import CategoricalIndex, Series, Timedelta, Timestamp
from pandas import CategoricalIndex, Series, Timedelta, Timestamp, date_range
import pandas._testing as tm
from pandas.core.arrays import (
DatetimeArray,
Expand Down Expand Up @@ -449,3 +449,39 @@ def test_to_numpy_dataframe_single_block_no_mutate():
expected = pd.DataFrame(np.array([1.0, 2.0, np.nan]))
result.to_numpy(na_value=0.0)
tm.assert_frame_equal(result, expected)


class TestAsArray:
@pytest.mark.parametrize("tz", [None, "US/Central"])
def test_asarray_object_dt64(self, tz):
ser = Series(date_range("2000", periods=2, tz=tz))

with tm.assert_produces_warning(None):
# Future behavior (for tzaware case) with no warning
result = np.asarray(ser, dtype=object)

expected = np.array(
[Timestamp("2000-01-01", tz=tz), Timestamp("2000-01-02", tz=tz)]
)
tm.assert_numpy_array_equal(result, expected)

def test_asarray_tz_naive(self):
# This shouldn't produce a warning.
ser = Series(date_range("2000", periods=2))
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
result = np.asarray(ser)

tm.assert_numpy_array_equal(result, expected)

def test_asarray_tz_aware(self):
tz = "US/Central"
ser = Series(date_range("2000", periods=2, tz=tz))
expected = np.array(["2000-01-01T06", "2000-01-02T06"], dtype="M8[ns]")
result = np.asarray(ser, dtype="datetime64[ns]")

tm.assert_numpy_array_equal(result, expected)

# Old behavior with no warning
result = np.asarray(ser, dtype="M8[ns]")

tm.assert_numpy_array_equal(result, expected)
17 changes: 16 additions & 1 deletion pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import pytest

from pandas import DataFrame, MultiIndex
from pandas import DataFrame, MultiIndex, period_range
import pandas._testing as tm


class TestGetitem:
Expand All @@ -14,3 +16,16 @@ def test_getitem_unused_level_raises(self):

with pytest.raises(KeyError, match="notevenone"):
df["notevenone"]

def test_getitem_periodindex(self):
rng = period_range("1/1/2000", periods=5)
df = DataFrame(np.random.randn(10, 5), columns=rng)

ts = df[rng[0]]
tm.assert_series_equal(ts, df.iloc[:, 0])

# GH#1211; smoketest unrelated to the rest of this test
repr(df)

ts = df["1/1/2000"]
tm.assert_series_equal(ts, df.iloc[:, 0])
18 changes: 18 additions & 0 deletions pandas/tests/frame/methods/test_reindex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import inspect
from itertools import permutations

import numpy as np
Expand Down Expand Up @@ -846,3 +847,20 @@ def test_reindex_with_categoricalindex(self):
df.reindex(["a"], level=1)
with pytest.raises(NotImplementedError, match=msg.format("limit")):
df.reindex(["a"], limit=2)

def test_reindex_signature(self):
sig = inspect.signature(DataFrame.reindex)
parameters = set(sig.parameters)
assert parameters == {
"self",
"labels",
"index",
"columns",
"axis",
"limit",
"copy",
"level",
"method",
"fill_value",
"tolerance",
}
16 changes: 16 additions & 0 deletions pandas/tests/frame/methods/test_rename.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import ChainMap
import inspect

import numpy as np
import pytest
Expand All @@ -8,6 +9,21 @@


class TestRename:
def test_rename_signature(self):
sig = inspect.signature(DataFrame.rename)
parameters = set(sig.parameters)
assert parameters == {
"self",
"mapper",
"index",
"columns",
"axis",
"inplace",
"copy",
"level",
"errors",
}

@pytest.mark.parametrize("klass", [Series, DataFrame])
def test_rename_mi(self, klass):
obj = klass(
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/methods/test_set_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from pandas import (
Categorical,
DataFrame,
DatetimeIndex,
Index,
Expand Down Expand Up @@ -372,6 +373,21 @@ def test_construction_with_categorical_index(self):
idf = idf.reset_index().set_index("B")
tm.assert_index_equal(idf.index, ci)

def test_set_index_preserve_categorical_dtype(self):
# GH#13743, GH#13854
df = DataFrame(
{
"A": [1, 2, 1, 1, 2],
"B": [10, 16, 22, 28, 34],
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False),
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True),
}
)
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]:
result = df.set_index(cols).reset_index()
result = result.reindex(columns=df.columns)
tm.assert_frame_equal(result, df)

def test_set_index_datetime(self):
# GH#3950
df = DataFrame(
Expand Down
76 changes: 0 additions & 76 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime
import inspect

import numpy as np
import pytest
Expand Down Expand Up @@ -137,34 +136,6 @@ def test_dti_set_index_reindex(self):

# Renaming

def test_reindex_api_equivalence(self):
# equivalence of the labels/axis and index/columns API's
df = DataFrame(
[[1, 2, 3], [3, 4, 5], [5, 6, 7]],
index=["a", "b", "c"],
columns=["d", "e", "f"],
)

res1 = df.reindex(["b", "a"])
res2 = df.reindex(index=["b", "a"])
res3 = df.reindex(labels=["b", "a"])
res4 = df.reindex(labels=["b", "a"], axis=0)
res5 = df.reindex(["b", "a"], axis=0)
for res in [res2, res3, res4, res5]:
tm.assert_frame_equal(res1, res)

res1 = df.reindex(columns=["e", "d"])
res2 = df.reindex(["e", "d"], axis=1)
res3 = df.reindex(labels=["e", "d"], axis=1)
for res in [res2, res3]:
tm.assert_frame_equal(res1, res)

res1 = df.reindex(index=["b", "a"], columns=["e", "d"])
res2 = df.reindex(columns=["e", "d"], index=["b", "a"])
res3 = df.reindex(labels=["b", "a"], axis=0).reindex(labels=["e", "d"], axis=1)
for res in [res2, res3]:
tm.assert_frame_equal(res1, res)

def test_assign_columns(self, float_frame):
float_frame["hi"] = "there"

Expand All @@ -173,38 +144,6 @@ def test_assign_columns(self, float_frame):
tm.assert_series_equal(float_frame["C"], df["baz"], check_names=False)
tm.assert_series_equal(float_frame["hi"], df["foo2"], check_names=False)

def test_rename_signature(self):
sig = inspect.signature(DataFrame.rename)
parameters = set(sig.parameters)
assert parameters == {
"self",
"mapper",
"index",
"columns",
"axis",
"inplace",
"copy",
"level",
"errors",
}

def test_reindex_signature(self):
sig = inspect.signature(DataFrame.reindex)
parameters = set(sig.parameters)
assert parameters == {
"self",
"labels",
"index",
"columns",
"axis",
"limit",
"copy",
"level",
"method",
"fill_value",
"tolerance",
}


class TestIntervalIndex:
def test_setitem(self):
Expand Down Expand Up @@ -256,21 +195,6 @@ def test_set_reset_index(self):


class TestCategoricalIndex:
def test_set_index_preserve_categorical_dtype(self):
# GH13743, GH13854
df = DataFrame(
{
"A": [1, 2, 1, 1, 2],
"B": [10, 16, 22, 28, 34],
"C1": Categorical(list("abaab"), categories=list("bac"), ordered=False),
"C2": Categorical(list("abaab"), categories=list("bac"), ordered=True),
}
)
for cols in ["C1", "C2", ["A", "C1"], ["A", "C2"], ["C1", "C2"]]:
result = df.set_index(cols).reset_index()
result = result.reindex(columns=df.columns)
tm.assert_frame_equal(result, df)

@pytest.mark.parametrize(
"codes", ([[0, 0, 1, 1], [0, 1, 0, 1]], [[0, 0, -1, 1], [0, 1, 0, 1]])
)
Expand Down
19 changes: 0 additions & 19 deletions pandas/tests/frame/test_period.py

This file was deleted.

7 changes: 0 additions & 7 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@


class TestSeriesAnalytics:
def test_ptp(self):
# GH21614
N = 1000
arr = np.random.randn(N)
ser = Series(arr)
assert np.ptp(ser) == np.ptp(arr)

def test_is_monotonic(self):

s = Series(np.random.randint(0, 10, size=1000))
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/series/test_npfuncs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Tests for np.foo applied to Series, not necessarily ufuncs.
"""

import numpy as np

from pandas import Series


class TestPtp:
def test_ptp(self):
# GH#21614
N = 1000
arr = np.random.randn(N)
ser = Series(arr)
assert np.ptp(ser) == np.ptp(arr)
35 changes: 0 additions & 35 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, Series, date_range, timedelta_range
Expand Down Expand Up @@ -70,37 +69,3 @@ def test_view_tz(self):
]
)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("tz", [None, "US/Central"])
def test_asarray_object_dt64(self, tz):
ser = Series(pd.date_range("2000", periods=2, tz=tz))

with tm.assert_produces_warning(None):
# Future behavior (for tzaware case) with no warning
result = np.asarray(ser, dtype=object)

expected = np.array(
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
)
tm.assert_numpy_array_equal(result, expected)

def test_asarray_tz_naive(self):
# This shouldn't produce a warning.
ser = Series(pd.date_range("2000", periods=2))
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
result = np.asarray(ser)

tm.assert_numpy_array_equal(result, expected)

def test_asarray_tz_aware(self):
tz = "US/Central"
ser = Series(pd.date_range("2000", periods=2, tz=tz))
expected = np.array(["2000-01-01T06", "2000-01-02T06"], dtype="M8[ns]")
result = np.asarray(ser, dtype="datetime64[ns]")

tm.assert_numpy_array_equal(result, expected)

# Old behavior with no warning
result = np.asarray(ser, dtype="M8[ns]")

tm.assert_numpy_array_equal(result, expected)