Skip to content

Commit 20bbd12

Browse files
authored
CLN: parametrize tests, comments, docstrings (#49051)
* CLN: parametrize tests, comments, docstrings * privatize * revert file with mypy complaints
1 parent c6cf37a commit 20bbd12

File tree

8 files changed

+55
-62
lines changed

8 files changed

+55
-62
lines changed

pandas/core/arrays/datetimes.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ def _from_sequence_not_strict(
310310
freq = freq if freq is not lib.no_default else None
311311
freq, freq_infer = dtl.maybe_infer_freq(freq)
312312

313+
# if the user either explicitly passes tz=None or a tz-naive dtype, we
314+
# disallows inferring a tz.
313315
explicit_tz_none = tz is None
314316
if tz is lib.no_default:
315317
tz = None
@@ -2023,9 +2025,8 @@ def _sequence_to_dt64ns(
20232025
Parameters
20242026
----------
20252027
data : list-like
2026-
dtype : dtype, str, or None, default None
20272028
copy : bool, default False
2028-
tz : tzinfo, str, or None, default None
2029+
tz : tzinfo or None, default None
20292030
dayfirst : bool, default False
20302031
yearfirst : bool, default False
20312032
ambiguous : str, bool, or arraylike, default 'raise'
@@ -2116,7 +2117,6 @@ def _sequence_to_dt64ns(
21162117

21172118
if tz is not None:
21182119
# Convert tz-naive to UTC
2119-
tz = timezones.maybe_get_tz(tz)
21202120
# TODO: if tz is UTC, are there situations where we *don't* want a
21212121
# copy? tz_localize_to_utc always makes one.
21222122
data = tzconversion.tz_localize_to_utc(
@@ -2130,9 +2130,6 @@ def _sequence_to_dt64ns(
21302130
else:
21312131
# must be integer dtype otherwise
21322132
# assume this data are epoch timestamps
2133-
if tz:
2134-
tz = timezones.maybe_get_tz(tz)
2135-
21362133
if data.dtype != INT64_DTYPE:
21372134
data = data.astype(np.int64, copy=False)
21382135
result = data.view(DT64NS_DTYPE)

pandas/core/arrays/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ def sequence_to_td64ns(
922922

923923
elif is_integer_dtype(data.dtype):
924924
# treat as multiples of the given unit
925-
data, copy_made = ints_to_td64ns(data, unit=unit)
925+
data, copy_made = _ints_to_td64ns(data, unit=unit)
926926
copy = copy and not copy_made
927927

928928
elif is_float_dtype(data.dtype):
@@ -959,7 +959,7 @@ def sequence_to_td64ns(
959959
return data, inferred_freq
960960

961961

962-
def ints_to_td64ns(data, unit: str = "ns"):
962+
def _ints_to_td64ns(data, unit: str = "ns"):
963963
"""
964964
Convert an ndarray with integer-dtype to timedelta64[ns] dtype, treating
965965
the integers as multiples of the given timedelta unit.

pandas/core/indexes/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def _wrap_range_setop(self, other, res_i8):
433433
new_freq = to_offset(Timedelta(res_i8.step))
434434
res_i8 = res_i8
435435

436-
# TODO: we cannot just do
436+
# TODO(GH#41493): we cannot just do
437437
# type(self._data)(res_i8.values, dtype=self.dtype, freq=new_freq)
438438
# because test_setops_preserve_freq fails with _validate_frequency raising.
439439
# This raising is incorrect, as 'on_freq' is incorrect. This will

pandas/core/reshape/merge.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,8 @@ def _maybe_add_join_keys(
911911
left_has_missing = None
912912
right_has_missing = None
913913

914+
assert all(is_array_like(x) for x in self.left_join_keys)
915+
914916
keys = zip(self.join_names, self.left_on, self.right_on)
915917
for i, (name, lname, rname) in enumerate(keys):
916918
if not _should_fill(lname, rname):
@@ -947,7 +949,7 @@ def _maybe_add_join_keys(
947949
):
948950
take_right = self.right[name]._values
949951

950-
elif left_indexer is not None and is_array_like(self.left_join_keys[i]):
952+
elif left_indexer is not None:
951953
take_left = self.left_join_keys[i]
952954
take_right = self.right_join_keys[i]
953955

pandas/tests/base/test_constructors.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@
2020
)
2121

2222

23+
def series_via_frame_from_dict(x, **kwargs):
24+
return DataFrame({"a": x}, **kwargs)["a"]
25+
26+
27+
def series_via_frame_from_scalar(x, **kwargs):
28+
return DataFrame(x, **kwargs)[0]
29+
30+
2331
@pytest.fixture(
2432
params=[
2533
Series,
26-
lambda x, **kwargs: DataFrame({"a": x}, **kwargs)["a"],
27-
lambda x, **kwargs: DataFrame(x, **kwargs)[0],
34+
series_via_frame_from_dict,
35+
series_via_frame_from_scalar,
2836
Index,
2937
],
3038
ids=["Series", "DataFrame-dict", "DataFrame-array", "Index"],
@@ -116,15 +124,6 @@ class TestConstruction:
116124
# test certain constructor behaviours on dtype inference across Series,
117125
# Index and DataFrame
118126

119-
@pytest.mark.parametrize(
120-
"klass",
121-
[
122-
Series,
123-
lambda x, **kwargs: DataFrame({"a": x}, **kwargs)["a"],
124-
lambda x, **kwargs: DataFrame(x, **kwargs)[0],
125-
Index,
126-
],
127-
)
128127
@pytest.mark.parametrize(
129128
"a",
130129
[
@@ -140,7 +139,7 @@ class TestConstruction:
140139
"object-string",
141140
],
142141
)
143-
def test_constructor_datetime_outofbound(self, a, klass):
142+
def test_constructor_datetime_outofbound(self, a, constructor):
144143
# GH-26853 (+ bug GH-26206 out of bound non-ns unit)
145144

146145
# No dtype specified (dtype inference)
@@ -149,17 +148,17 @@ def test_constructor_datetime_outofbound(self, a, klass):
149148
if a.dtype.kind == "M":
150149
msg = "Out of bounds"
151150
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
152-
klass(a)
151+
constructor(a)
153152
else:
154-
result = klass(a)
153+
result = constructor(a)
155154
assert result.dtype == "object"
156155
tm.assert_numpy_array_equal(result.to_numpy(), a)
157156

158157
# Explicit dtype specified
159158
# Forced conversion fails for all -> all cases raise error
160159
msg = "Out of bounds|Out of bounds .* present at position 0"
161160
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
162-
klass(a, dtype="datetime64[ns]")
161+
constructor(a, dtype="datetime64[ns]")
163162

164163
def test_constructor_datetime_nonns(self, constructor):
165164
arr = np.array(["2020-01-01T00:00:00.000000"], dtype="datetime64[us]")

pandas/tests/indexes/datetimes/test_constructors.py

+1
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ def test_constructor_dtype(self):
696696
idx = DatetimeIndex(["2013-01-01", "2013-01-02"], tz="US/Eastern")
697697
tm.assert_index_equal(idx, expected)
698698

699+
def test_constructor_dtype_tz_mismatch_raises(self):
699700
# if we already have a tz and its not the same, then raise
700701
idx = DatetimeIndex(
701702
["2013-01-01", "2013-01-02"], dtype="datetime64[ns, US/Eastern]"

pandas/tests/reshape/merge/test_merge.py

+16-24
Original file line numberDiff line numberDiff line change
@@ -723,35 +723,27 @@ def test_join_append_timedeltas2(self):
723723
)
724724
tm.assert_frame_equal(result, expected)
725725

726-
def test_other_datetime_unit(self):
726+
@pytest.mark.parametrize("unit", ["D", "h", "m", "s", "ms", "us", "ns"])
727+
def test_other_datetime_unit(self, unit):
727728
# GH 13389
728729
df1 = DataFrame({"entity_id": [101, 102]})
729-
s = Series([None, None], index=[101, 102], name="days")
730-
731-
for dtype in [
732-
"datetime64[D]",
733-
"datetime64[h]",
734-
"datetime64[m]",
735-
"datetime64[s]",
736-
"datetime64[ms]",
737-
"datetime64[us]",
738-
"datetime64[ns]",
739-
]:
730+
ser = Series([None, None], index=[101, 102], name="days")
740731

741-
df2 = s.astype(dtype).to_frame("days")
742-
# coerces to datetime64[ns], thus should not be affected
743-
assert df2["days"].dtype == "datetime64[ns]"
732+
dtype = f"datetime64[{unit}]"
733+
df2 = ser.astype(dtype).to_frame("days")
734+
# coerces to datetime64[ns], thus should not be affected
735+
assert df2["days"].dtype == "datetime64[ns]"
744736

745-
result = df1.merge(df2, left_on="entity_id", right_index=True)
737+
result = df1.merge(df2, left_on="entity_id", right_index=True)
746738

747-
exp = DataFrame(
748-
{
749-
"entity_id": [101, 102],
750-
"days": np.array(["nat", "nat"], dtype="datetime64[ns]"),
751-
},
752-
columns=["entity_id", "days"],
753-
)
754-
tm.assert_frame_equal(result, exp)
739+
exp = DataFrame(
740+
{
741+
"entity_id": [101, 102],
742+
"days": np.array(["nat", "nat"], dtype="datetime64[ns]"),
743+
},
744+
columns=["entity_id", "days"],
745+
)
746+
tm.assert_frame_equal(result, exp)
755747

756748
@pytest.mark.parametrize("unit", ["D", "h", "m", "s", "ms", "us", "ns"])
757749
def test_other_timedelta_unit(self, unit):

pandas/tests/series/test_constructors.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -921,21 +921,18 @@ def test_constructor_dtype_datetime64(self):
921921

922922
def test_constructor_dtype_datetime64_10(self):
923923
# GH3416
924-
dates = [
925-
np.datetime64(datetime(2013, 1, 1)),
926-
np.datetime64(datetime(2013, 1, 2)),
927-
np.datetime64(datetime(2013, 1, 3)),
928-
]
924+
pydates = [datetime(2013, 1, 1), datetime(2013, 1, 2), datetime(2013, 1, 3)]
925+
dates = [np.datetime64(x) for x in pydates]
929926

930-
s = Series(dates)
931-
assert s.dtype == "M8[ns]"
927+
ser = Series(dates)
928+
assert ser.dtype == "M8[ns]"
932929

933-
s.iloc[0] = np.nan
934-
assert s.dtype == "M8[ns]"
930+
ser.iloc[0] = np.nan
931+
assert ser.dtype == "M8[ns]"
935932

936933
# GH3414 related
937934
expected = Series(
938-
[datetime(2013, 1, 1), datetime(2013, 1, 2), datetime(2013, 1, 3)],
935+
pydates,
939936
dtype="datetime64[ns]",
940937
)
941938

@@ -951,6 +948,10 @@ def test_constructor_dtype_datetime64_10(self):
951948
result = Series([np.nan] + dates[1:], dtype="datetime64[ns]")
952949
tm.assert_series_equal(result, expected)
953950

951+
def test_constructor_dtype_datetime64_11(self):
952+
pydates = [datetime(2013, 1, 1), datetime(2013, 1, 2), datetime(2013, 1, 3)]
953+
dates = [np.datetime64(x) for x in pydates]
954+
954955
dts = Series(dates, dtype="datetime64[ns]")
955956

956957
# valid astype
@@ -1151,12 +1152,13 @@ def test_constructor_no_partial_datetime_casting(self):
11511152
assert all(ser[i] is vals[i] for i in range(len(vals)))
11521153

11531154
@pytest.mark.parametrize("arr_dtype", [np.int64, np.float64])
1154-
@pytest.mark.parametrize("dtype", ["M8", "m8"])
1155+
@pytest.mark.parametrize("kind", ["M", "m"])
11551156
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s", "h", "m", "D"])
1156-
def test_construction_to_datetimelike_unit(self, arr_dtype, dtype, unit):
1157+
def test_construction_to_datetimelike_unit(self, arr_dtype, kind, unit):
11571158
# tests all units
11581159
# gh-19223
1159-
dtype = f"{dtype}[{unit}]"
1160+
# TODO: GH#19223 was about .astype, doesn't belong here
1161+
dtype = f"{kind}8[{unit}]"
11601162
arr = np.array([1, 2, 3], dtype=arr_dtype)
11611163
s = Series(arr)
11621164
result = s.astype(dtype)

0 commit comments

Comments
 (0)