diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index 7867d882befa0..538cf2c78b50e 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -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, @@ -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) diff --git a/pandas/tests/frame/indexing/test_getitem.py b/pandas/tests/frame/indexing/test_getitem.py index 47d53ef6f3619..d9cdfa5ea45ec 100644 --- a/pandas/tests/frame/indexing/test_getitem.py +++ b/pandas/tests/frame/indexing/test_getitem.py @@ -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: @@ -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]) diff --git a/pandas/tests/frame/methods/test_reindex.py b/pandas/tests/frame/methods/test_reindex.py index 9e50d2889f9ad..bc3750a196c5f 100644 --- a/pandas/tests/frame/methods/test_reindex.py +++ b/pandas/tests/frame/methods/test_reindex.py @@ -1,4 +1,5 @@ from datetime import datetime +import inspect from itertools import permutations import numpy as np @@ -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", + } diff --git a/pandas/tests/frame/methods/test_rename.py b/pandas/tests/frame/methods/test_rename.py index ccd365044942a..857dd0ad7268b 100644 --- a/pandas/tests/frame/methods/test_rename.py +++ b/pandas/tests/frame/methods/test_rename.py @@ -1,4 +1,5 @@ from collections import ChainMap +import inspect import numpy as np import pytest @@ -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( diff --git a/pandas/tests/frame/methods/test_set_index.py b/pandas/tests/frame/methods/test_set_index.py index 29cd3c2d535d9..aea8caff5936b 100644 --- a/pandas/tests/frame/methods/test_set_index.py +++ b/pandas/tests/frame/methods/test_set_index.py @@ -4,6 +4,7 @@ import pytest from pandas import ( + Categorical, DataFrame, DatetimeIndex, Index, @@ -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( diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index deac1792737a1..e4a0d37e3a017 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -1,5 +1,4 @@ from datetime import datetime -import inspect import numpy as np import pytest @@ -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" @@ -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): @@ -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]]) ) diff --git a/pandas/tests/frame/test_period.py b/pandas/tests/frame/test_period.py deleted file mode 100644 index cf467e3eda60a..0000000000000 --- a/pandas/tests/frame/test_period.py +++ /dev/null @@ -1,19 +0,0 @@ -import numpy as np - -from pandas import DataFrame, period_range -import pandas._testing as tm - - -class TestPeriodIndex: - def test_as_frame_columns(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 - repr(df) - - ts = df["1/1/2000"] - tm.assert_series_equal(ts, df.iloc[:, 0]) diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index 527feb6537e75..ebb75adde5b13 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -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)) diff --git a/pandas/tests/series/test_npfuncs.py b/pandas/tests/series/test_npfuncs.py new file mode 100644 index 0000000000000..645a849015c23 --- /dev/null +++ b/pandas/tests/series/test_npfuncs.py @@ -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) diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py index 295f70935a786..8b32be45e8d57 100644 --- a/pandas/tests/series/test_timeseries.py +++ b/pandas/tests/series/test_timeseries.py @@ -1,5 +1,4 @@ import numpy as np -import pytest import pandas as pd from pandas import DataFrame, Series, date_range, timedelta_range @@ -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)