Skip to content

WIP: TST: pass random names in tm.makeDateIndex #31227

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

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 21 additions & 0 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,27 @@ cdef inline bint is_null_period(v):
return checknull_with_nat(v)


def is_matching_na(left, right) -> bool:
"""
Check if two NA-like scalars represent the same type of NA.
"""
if left is None:
return right is None
if left is C_NA:
return right is C_NA
if left is NaT:
return right is NaT
if util.is_nan(left):
if not util.is_nan(right):
return False
if util.is_float_object(left):
return util.is_float_object(right)
if util.is_complex_object(left):
return util.is_complex_object(right)
raise NotImplementedError(left, right)
raise NotImplementedError(left, right)


# -----------------------------------------------------------------------------
# Implementation of NA singleton

Expand Down
37 changes: 34 additions & 3 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
set_locale,
)

from pandas._libs.lib import no_default
import pandas._libs.testing as _testing
from pandas._typing import FilePathOrBuffer, FrameOrSeries
from pandas.compat import _get_lzma_file, _import_lzma
Expand Down Expand Up @@ -1575,6 +1576,30 @@ def getCols(k):
return string.ascii_uppercase[:k]


def make_any_name():
"""
Return a random hashable object.
"""
return np.random.choice(
[
None,
"a",
"foobar" * 100,
1,
2.0,
np.float64(3),
np.nan,
pd.NaT,
pd.NA,
pd.Timestamp.now(),
pd.Timestamp.now("UTC"),
pd.Timedelta(minutes=45678),
pd.Timedelta(minutes=45678).to_timedelta64(),
pd.Timedelta(minutes=45678).to_pytimedelta(),
]
)


# make index
def makeStringIndex(k=10, name=None):
return Index(rands_array(nchars=10, size=k), name=name)
Expand Down Expand Up @@ -1623,17 +1648,23 @@ def makeFloatIndex(k=10, name=None):
return Index(values * (10 ** np.random.randint(0, 9)), name=name)


def makeDateIndex(k=10, freq="B", name=None, **kwargs):
def makeDateIndex(k=10, freq="B", name=no_default, **kwargs):
if name is no_default:
name = make_any_name()
dt = datetime(2000, 1, 1)
dr = bdate_range(dt, periods=k, freq=freq, name=name)
return DatetimeIndex(dr, name=name, **kwargs)


def makeTimedeltaIndex(k=10, freq="D", name=None, **kwargs):
def makeTimedeltaIndex(k=10, freq="D", name=no_default, **kwargs):
if name is no_default:
name = make_any_name()
return pd.timedelta_range(start="1 day", periods=k, freq=freq, name=name, **kwargs)


def makePeriodIndex(k=10, name=None, **kwargs):
def makePeriodIndex(k=10, name=no_default, **kwargs):
if name is no_default:
name = make_any_name()
dt = datetime(2000, 1, 1)
dr = pd.period_range(start=dt, periods=k, freq="B", name=name, **kwargs)
return dr
Expand Down
40 changes: 29 additions & 11 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

import numpy as np

from pandas._libs import algos as libalgos, index as libindex, lib
from pandas._libs import (
algos as libalgos,
index as libindex,
lib,
missing as libmissing,
)
import pandas._libs.join as libjoin
from pandas._libs.lib import is_datetime_array
from pandas._libs.tslibs import OutOfBoundsDatetime, Timestamp
Expand Down Expand Up @@ -4333,16 +4338,29 @@ def identical(self, other) -> bool:
If two Index objects have equal elements and same type True,
otherwise False.
"""
return (
self.equals(other)
and all(
(
getattr(self, c, None) == getattr(other, c, None)
for c in self._comparables
)
)
and type(self) == type(other)
)
if type(self) != type(other):
return False
if not self.equals(other):
return False
for attr in self._comparables:
left = getattr(self, attr)
right = getattr(other, attr)
if is_scalar(left) and isna(left):
# We have to avoid pd.NA raising TypeError when checking equality
if not libmissing.is_matching_na(left, right):
return False
continue
elif is_scalar(right) and isna(right):
return False
else:
try:
if not left == right:
return False
except TypeError:
# Timestamp tzawareness compat, Period freq compat
return False

return True

def asof(self, label):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def test_ensure_copied_data(self, indices):
return

index_type = type(indices)
result = index_type(indices.values, copy=True, **init_kwargs)
result = index_type(indices.values, copy=True, name=indices.name, **init_kwargs)
tm.assert_index_equal(indices, result)
tm.assert_numpy_array_equal(
indices._ndarray_values, result._ndarray_values, check_same="copy"
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ def test_map_tseries_indices_return_index(self, attr):
index = getattr(tm, attr)(10)
expected = Index([1] * 10)
result = index.map(lambda x: 1)
tm.assert_index_equal(expected, result)
tm.assert_index_equal(expected, result, check_names=False)

def test_map_tseries_indices_accsr_return_index(self):
date_index = tm.makeDateIndex(24, freq="h", name="hourly")
Expand Down Expand Up @@ -1126,7 +1126,7 @@ def test_map_dictlike(self, indices, mapper):
expected = Index(np.arange(len(indices), 0, -1))

result = indices.map(mapper(expected, indices))
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, check_names=False)

@pytest.mark.parametrize(
"mapper",
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_droplevel(self, indices):
for level in "wrong", ["wrong"]:
with pytest.raises(
KeyError,
match=r"'Requested level \(wrong\) does not match index name \(None\)'",
match=r"'Requested level \(wrong\) does not match index name \(.*\)'",
):
indices.droplevel(level)

Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ def test_dataframe(self):
_, ax = self.plt.subplots()
bts.plot(ax=ax)
idx = ax.get_lines()[0].get_xdata()
tm.assert_index_equal(bts.index.to_period(), PeriodIndex(idx))
tm.assert_index_equal(
bts.index.to_period(), PeriodIndex(idx, name=bts.index.name)
)

@pytest.mark.slow
def test_axis_limits(self):
Expand Down Expand Up @@ -692,8 +694,8 @@ def test_mixed_freq_regular_first(self):

ax2 = s2.plot(style="g", ax=ax)
lines = ax2.get_lines()
idx1 = PeriodIndex(lines[0].get_xdata())
idx2 = PeriodIndex(lines[1].get_xdata())
idx1 = PeriodIndex(lines[0].get_xdata(), name=s1.index.name)
idx2 = PeriodIndex(lines[1].get_xdata(), name=s2.index.name)

tm.assert_index_equal(idx1, s1.index.to_period("B"))
tm.assert_index_equal(idx2, s2.index.to_period("B"))
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/methods/test_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ def test_tshift(self, datetime_series):
tm.assert_series_equal(shifted, shifted2)

inferred_ts = Series(
datetime_series.values, Index(np.asarray(datetime_series.index)), name="ts"
datetime_series.values,
Index(np.asarray(datetime_series.index), name=datetime_series.index.name),
name="ts",
)
shifted = inferred_ts.tshift(1)
unshifted = shifted.tshift(-1)
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/series/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class TestSeriesToDict:
def test_to_dict(self, mapping, datetime_series):
# GH#16122
tm.assert_series_equal(
Series(datetime_series.to_dict(mapping), name="ts"), datetime_series
Series(datetime_series.to_dict(mapping), name="ts"),
datetime_series,
check_names=False,
)
from_method = Series(datetime_series.to_dict(collections.Counter))
from_constructor = Series(collections.Counter(datetime_series.items()))
tm.assert_series_equal(from_method, from_constructor)
tm.assert_series_equal(from_method, from_constructor, check_names=False)
3 changes: 2 additions & 1 deletion pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ def test_map_empty(self, index):
result = s.map({})

expected = pd.Series(np.nan, index=s.index)
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result, expected, check_names=False)
# TODO: can we check names her?
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# TODO: can we check names her?
# TODO: can we check names here?


def test_map_compat(self):
# related GH 8024
Expand Down