Skip to content

TST: collect Index tests by method #40070

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 13 commits into from
Feb 26, 2021
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
2 changes: 1 addition & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def _aggregate_series_pure_python(self, obj: Series, func: F):
counts[label] = group.shape[0]
result[label] = res

result = lib.maybe_convert_objects(result, try_float=0)
result = lib.maybe_convert_objects(result, try_float=False)
result = maybe_cast_result(result, obj, numeric_only=True)

return result, counts
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ def _format_with_header(
values = self._values

if is_object_dtype(values.dtype):
values = lib.maybe_convert_objects(values, safe=1)
values = lib.maybe_convert_objects(values, safe=True)

result = [pprint_thing(x, escape_chars=("\t", "\r", "\n")) for x in values]

Expand Down
16 changes: 11 additions & 5 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,23 @@ def test_maybe_convert_objects_datetime(self):
[np.datetime64("2000-01-01"), np.timedelta64(1, "s")], dtype=object
)
exp = arr.copy()
out = lib.maybe_convert_objects(arr, convert_datetime=1, convert_timedelta=1)
out = lib.maybe_convert_objects(
arr, convert_datetime=True, convert_timedelta=True
)
tm.assert_numpy_array_equal(out, exp)

arr = np.array([pd.NaT, np.timedelta64(1, "s")], dtype=object)
exp = np.array([np.timedelta64("NaT"), np.timedelta64(1, "s")], dtype="m8[ns]")
out = lib.maybe_convert_objects(arr, convert_datetime=1, convert_timedelta=1)
out = lib.maybe_convert_objects(
arr, convert_datetime=True, convert_timedelta=True
)
tm.assert_numpy_array_equal(out, exp)

arr = np.array([np.timedelta64(1, "s"), np.nan], dtype=object)
exp = arr.copy()
out = lib.maybe_convert_objects(arr, convert_datetime=1, convert_timedelta=1)
out = lib.maybe_convert_objects(
arr, convert_datetime=True, convert_timedelta=True
)
tm.assert_numpy_array_equal(out, exp)

@pytest.mark.parametrize(
Expand All @@ -587,7 +593,7 @@ def test_maybe_convert_objects_datetime(self):
def test_maybe_convert_objects_nullable_integer(self, exp):
# GH27335
arr = np.array([2, np.NaN], dtype=object)
result = lib.maybe_convert_objects(arr, convert_to_nullable_integer=1)
result = lib.maybe_convert_objects(arr, convert_to_nullable_integer=True)

tm.assert_extension_array_equal(result, exp)

Expand All @@ -601,7 +607,7 @@ def test_maybe_convert_objects_bool_nan(self):
def test_mixed_dtypes_remain_object_array(self):
# GH14956
array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object)
result = lib.maybe_convert_objects(array, convert_datetime=1)
result = lib.maybe_convert_objects(array, convert_datetime=True)
tm.assert_numpy_array_equal(result, array)


Expand Down
80 changes: 80 additions & 0 deletions pandas/tests/indexes/datetimelike_/test_drop_duplicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import numpy as np
import pytest

from pandas import (
PeriodIndex,
Series,
date_range,
period_range,
timedelta_range,
)
import pandas._testing as tm


class DropDuplicates:
def test_drop_duplicates_metadata(self, idx):
# GH#10115
result = idx.drop_duplicates()
tm.assert_index_equal(idx, result)
assert idx.freq == result.freq

idx_dup = idx.append(idx)
result = idx_dup.drop_duplicates()

expected = idx
if not isinstance(idx, PeriodIndex):
# freq is reset except for PeriodIndex
assert idx_dup.freq is None
assert result.freq is None
expected = idx._with_freq(None)
else:
assert result.freq == expected.freq

tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"keep, expected, index",
[
("first", np.concatenate(([False] * 10, [True] * 5)), np.arange(0, 10)),
("last", np.concatenate(([True] * 5, [False] * 10)), np.arange(5, 15)),
(
False,
np.concatenate(([True] * 5, [False] * 5, [True] * 5)),
np.arange(5, 10),
),
],
)
def test_drop_duplicates(self, keep, expected, index, idx):
# to check Index/Series compat
idx = idx.append(idx[:5])

tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected)
expected = idx[~expected]

result = idx.drop_duplicates(keep=keep)
tm.assert_index_equal(result, expected)

result = Series(idx).drop_duplicates(keep=keep)
tm.assert_series_equal(result, Series(expected, index=index))


class TestDropDuplicatesPeriodIndex(DropDuplicates):
@pytest.fixture(params=["D", "3D", "H", "2H", "T", "2T", "S", "3S"])
def freq(self, request):
return request.param

@pytest.fixture
def idx(self, freq):
return period_range("2011-01-01", periods=10, freq=freq, name="idx")


class TestDropDuplicatesDatetimeIndex(DropDuplicates):
@pytest.fixture
def idx(self, freq_sample):
return date_range("2011-01-01", freq=freq_sample, periods=10, name="idx")


class TestDropDuplicatesTimedeltaIndex(DropDuplicates):
@pytest.fixture
def idx(self, freq_sample):
return timedelta_range("1 day", periods=10, freq=freq_sample, name="idx")
54 changes: 54 additions & 0 deletions pandas/tests/indexes/datetimelike_/test_nat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy as np
import pytest

from pandas import (
DatetimeIndex,
NaT,
PeriodIndex,
TimedeltaIndex,
)
import pandas._testing as tm


class NATests:
def test_nat(self, index_without_na):
empty_index = index_without_na[:0]

index_with_na = index_without_na.copy(deep=True)
index_with_na._data[1] = NaT

assert type(index_without_na)._na_value is NaT
assert empty_index._na_value is NaT
assert index_with_na._na_value is NaT
assert index_without_na._na_value is NaT

idx = index_without_na
assert idx._can_hold_na

tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
assert idx.hasnans is False

idx = index_with_na
assert idx._can_hold_na

tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
assert idx.hasnans is True


class TestDatetimeIndexNA(NATests):
@pytest.fixture
def index_without_na(self, tz_naive_fixture):
tz = tz_naive_fixture
return DatetimeIndex(["2011-01-01", "2011-01-02"], tz=tz)


class TestTimedeltaIndexNA(NATests):
@pytest.fixture
def index_without_na(self):
return TimedeltaIndex(["1 days", "2 days"])


class TestPeriodIndexNA(NATests):
@pytest.fixture
def index_without_na(self):
return PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
Loading