Skip to content

Commit bac5764

Browse files
committed
Refinements
1 parent 919536d commit bac5764

File tree

9 files changed

+93
-68
lines changed

9 files changed

+93
-68
lines changed

pandas/core/dtypes/cast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,12 @@ def maybe_unbox_numpy_scalar(value):
14431443
if using_python_scalars() and isinstance(value, np.generic):
14441444
if isinstance(result, np.longdouble):
14451445
result = float(result)
1446+
elif isinstance(result, np.complex256):
1447+
result = complex(result)
1448+
elif isinstance(result, np.datetime64):
1449+
result = Timestamp(result)
1450+
elif isinstance(result, np.timedelta64):
1451+
result = Timedelta(result)
14461452
else:
14471453
result = value.item()
14481454
return result

pandas/tests/arrays/floating/test_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def test_preserve_dtypes(op, using_python_scalars):
183183
# op
184184
result = getattr(df.C, op)()
185185
if using_python_scalars:
186-
assert isinstance(result, float)
186+
assert type(result) == float
187187
else:
188188
assert isinstance(result, np.float64)
189189

pandas/tests/dtypes/cast/test_maybe_box_native.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
from datetime import datetime
1+
from datetime import (
2+
datetime,
3+
timedelta,
4+
)
25

36
import numpy as np
47
import pytest
58

6-
from pandas.core.dtypes.cast import maybe_box_native
9+
from pandas.core.dtypes.cast import (
10+
maybe_box_native,
11+
maybe_unbox_numpy_scalar,
12+
)
713

814
from pandas import (
915
Interval,
@@ -38,3 +44,61 @@ def test_maybe_box_native(obj, expected_dtype):
3844
boxed_obj = maybe_box_native(obj)
3945
result_dtype = type(boxed_obj)
4046
assert result_dtype is expected_dtype
47+
48+
49+
@pytest.mark.parametrize("typecode", np.typecodes["All"])
50+
def test_maybe_unbox_numpy_scalar(typecode, using_python_scalars):
51+
# https://github.com/pandas-dev/pandas/pull/63016
52+
if typecode == "?":
53+
scalar = False
54+
expected = bool
55+
elif typecode in "bhilqnpBHILQNP":
56+
scalar = 0
57+
expected = int
58+
elif typecode in "efdg":
59+
scalar = 0.0
60+
expected = float
61+
elif typecode in "FDG":
62+
scalar = 0.0 + 0.0j
63+
expected = complex
64+
elif typecode in "SV":
65+
scalar = b""
66+
expected = bytes
67+
elif typecode == "U":
68+
scalar = ""
69+
expected = str
70+
elif typecode == "O":
71+
scalar = 0
72+
expected = int
73+
elif typecode == "M":
74+
scalar = datetime(2025, 1, 1)
75+
expected = Timestamp
76+
elif typecode == "m":
77+
scalar = timedelta(seconds=3)
78+
expected = Timedelta
79+
else:
80+
raise ValueError(f"typecode {typecode} not recognized")
81+
value = np.array([scalar], dtype=typecode)[0]
82+
result = maybe_unbox_numpy_scalar(value)
83+
if using_python_scalars:
84+
assert type(result) == expected
85+
else:
86+
assert result is value
87+
88+
89+
def test_maybe_unbox_numpy_scalar_timestamp(unit, using_python_scalars):
90+
# https://github.com/pandas-dev/pandas/pull/63016
91+
value = np.datetime64(1, unit)
92+
expected = Timestamp(1, unit=unit) if using_python_scalars else value
93+
result = maybe_unbox_numpy_scalar(value)
94+
assert result == expected
95+
assert type(result) == type(expected)
96+
97+
98+
def test_maybe_unbox_numpy_scalar_datetime(unit, using_python_scalars):
99+
# https://github.com/pandas-dev/pandas/pull/63016
100+
value = np.timedelta64(1, unit)
101+
expected = Timedelta(1, unit=unit) if using_python_scalars else value
102+
result = maybe_unbox_numpy_scalar(value)
103+
assert result == expected
104+
assert type(result) == type(expected)

pandas/tests/reductions/test_reductions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ def test_signedness_preserved_after_sum(self, using_python_scalars):
12761276
if using_python_scalars:
12771277
assert isinstance(result, int)
12781278
else:
1279-
assert result.dtype == "uint64"
1279+
assert isinstance(result, np.uint64)
12801280

12811281

12821282
class TestDatetime64SeriesReductions:

pandas/tests/reductions/test_stat_reductions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def test_skew(self, using_python_scalars):
248248
else:
249249
assert 0 == s.skew()
250250
if using_python_scalars:
251-
assert isinstance(s.skew(), float)
251+
assert type(s.skew()) == float
252252
else:
253253
assert isinstance(s.skew(), np.float64) # GH53482
254254
assert (df.skew() == 0).all()

pandas/tests/series/test_ufunc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def test_max(self, values_for_np_reduce, box_with_array, using_python_scalars):
335335
values = values_for_np_reduce
336336

337337
same_type = True
338-
if box is pd.Index and values.dtype.kind in ["i", "f"]:
338+
if box is pd.Index and values.dtype.kind in "if":
339339
# ATM Index casts to object, so we get python ints/floats
340340
same_type = False
341341

@@ -349,7 +349,7 @@ def test_max(self, values_for_np_reduce, box_with_array, using_python_scalars):
349349
tm.assert_series_equal(result, expected)
350350
else:
351351
expected = values[1]
352-
if using_python_scalars and values.dtype.kind in ["i", "f"]:
352+
if using_python_scalars and values.dtype.kind in "if":
353353
expected = expected.item()
354354
assert result == expected
355355
if same_type:
@@ -361,7 +361,7 @@ def test_min(self, values_for_np_reduce, box_with_array, using_python_scalars):
361361
values = values_for_np_reduce
362362

363363
same_type = True
364-
if box is pd.Index and values.dtype.kind in ["i", "f"]:
364+
if box is pd.Index and values.dtype.kind in "if":
365365
# ATM Index casts to object, so we get python ints/floats
366366
same_type = False
367367

pandas/tests/window/moments/test_moments_consistency_ewm.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ def test_moments_consistency_var(all_data, adjust, ignore_na, min_periods, bias)
133133
var_x = all_data.ewm(
134134
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
135135
).var(bias=bias)
136-
if isinstance(all_data, Series):
137-
assert not (var_x < 0).any()
138-
else:
139-
assert not (var_x < 0).any().any()
136+
assert not (var_x < 0).any(axis=None)
140137

141138
if bias:
142139
# check that biased var(x) == mean(x^2) - mean(x)^2
@@ -159,10 +156,7 @@ def test_moments_consistency_var_constant(
159156
).var(bias=bias)
160157

161158
# check that variance of constant series is identically 0
162-
if isinstance(consistent_data, Series):
163-
assert not (var_x > 0).any()
164-
else:
165-
assert not (var_x > 0).any().any()
159+
assert not (var_x > 0).any(axis=None)
166160
expected = consistent_data * np.nan
167161
expected[count_x >= max(min_periods, 1)] = 0.0
168162
if not bias:
@@ -176,29 +170,20 @@ def test_ewm_consistency_std(all_data, adjust, ignore_na, min_periods, bias):
176170
var_x = all_data.ewm(
177171
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
178172
).var(bias=bias)
179-
if isinstance(all_data, Series):
180-
assert not (var_x < 0).any()
181-
else:
182-
assert not (var_x < 0).any().any()
173+
assert not (var_x < 0).any(axis=None)
183174

184175
std_x = all_data.ewm(
185176
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
186177
).std(bias=bias)
187-
if isinstance(all_data, Series):
188-
assert not (std_x < 0).any()
189-
else:
190-
assert not (std_x < 0).any().any()
178+
assert not (std_x < 0).any(axis=None)
191179

192180
# check that var(x) == std(x)^2
193181
tm.assert_equal(var_x, std_x * std_x)
194182

195183
cov_x_x = all_data.ewm(
196184
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
197185
).cov(all_data, bias=bias)
198-
if isinstance(all_data, Series):
199-
assert not (cov_x_x < 0).any()
200-
else:
201-
assert not (cov_x_x < 0).any().any()
186+
assert not (cov_x_x < 0).any(axis=None)
202187

203188
# check that var(x) == cov(x, x)
204189
tm.assert_equal(var_x, cov_x_x)

pandas/tests/window/moments/test_moments_consistency_expanding.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ def test_expanding_apply_consistency_sum_nans(request, all_data, min_periods, f)
3838
@pytest.mark.parametrize("ddof", [0, 1])
3939
def test_moments_consistency_var(all_data, min_periods, ddof):
4040
var_x = all_data.expanding(min_periods=min_periods).var(ddof=ddof)
41-
if isinstance(all_data, Series):
42-
assert not (var_x < 0).any()
43-
else:
44-
assert not (var_x < 0).any().any()
41+
assert not (var_x < 0).any(axis=None)
4542

4643
if ddof == 0:
4744
# check that biased var(x) == mean(x^2) - mean(x)^2
@@ -56,10 +53,7 @@ def test_moments_consistency_var_constant(consistent_data, min_periods, ddof):
5653
var_x = consistent_data.expanding(min_periods=min_periods).var(ddof=ddof)
5754

5855
# check that variance of constant series is identically 0
59-
if isinstance(consistent_data, Series):
60-
assert not (var_x > 0).any()
61-
else:
62-
assert not (var_x > 0).any().any()
56+
assert not (var_x > 0).any(axis=None)
6357
expected = consistent_data * np.nan
6458
expected[count_x >= max(min_periods, 1)] = 0.0
6559
if ddof == 1:
@@ -70,25 +64,16 @@ def test_moments_consistency_var_constant(consistent_data, min_periods, ddof):
7064
@pytest.mark.parametrize("ddof", [0, 1])
7165
def test_expanding_consistency_var_std_cov(all_data, min_periods, ddof):
7266
var_x = all_data.expanding(min_periods=min_periods).var(ddof=ddof)
73-
if isinstance(all_data, Series):
74-
assert not (var_x < 0).any()
75-
else:
76-
assert not (var_x < 0).any().any()
67+
assert not (var_x < 0).any(axis=None)
7768

7869
std_x = all_data.expanding(min_periods=min_periods).std(ddof=ddof)
79-
if isinstance(all_data, Series):
80-
assert not (std_x < 0).any()
81-
else:
82-
assert not (std_x < 0).any().any()
70+
assert not (std_x < 0).any(axis=None)
8371

8472
# check that var(x) == std(x)^2
8573
tm.assert_equal(var_x, std_x * std_x)
8674

8775
cov_x_x = all_data.expanding(min_periods=min_periods).cov(all_data, ddof=ddof)
88-
if isinstance(all_data, Series):
89-
assert not (cov_x_x < 0).any()
90-
else:
91-
assert not (cov_x_x < 0).any().any()
76+
assert not (cov_x_x < 0).any(axis=None)
9277

9378
# check that var(x) == cov(x, x)
9479
tm.assert_equal(var_x, cov_x_x)

pandas/tests/window/moments/test_moments_consistency_rolling.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ def test_moments_consistency_var(all_data, rolling_consistency_cases, center, dd
5656
var_x = all_data.rolling(window=window, min_periods=min_periods, center=center).var(
5757
ddof=ddof
5858
)
59-
if isinstance(all_data, Series):
60-
assert not (var_x < 0).any()
61-
else:
62-
assert not (var_x < 0).any().any()
59+
assert not (var_x < 0).any(axis=None)
6360

6461
if ddof == 0:
6562
# check that biased var(x) == mean(x^2) - mean(x)^2
@@ -88,10 +85,7 @@ def test_moments_consistency_var_constant(
8885
).var(ddof=ddof)
8986

9087
# check that variance of constant series is identically 0
91-
if isinstance(consistent_data, Series):
92-
assert not (var_x > 0).any()
93-
else:
94-
assert not (var_x > 0).any().any()
88+
assert not (var_x > 0).any(axis=None)
9589
expected = consistent_data * np.nan
9690
expected[count_x >= max(min_periods, 1)] = 0.0
9791
if ddof == 1:
@@ -108,29 +102,20 @@ def test_rolling_consistency_var_std_cov(
108102
var_x = all_data.rolling(window=window, min_periods=min_periods, center=center).var(
109103
ddof=ddof
110104
)
111-
if isinstance(all_data, Series):
112-
assert not (var_x < 0).any()
113-
else:
114-
assert not (var_x < 0).any().any()
105+
assert not (var_x < 0).any(axis=None)
115106

116107
std_x = all_data.rolling(window=window, min_periods=min_periods, center=center).std(
117108
ddof=ddof
118109
)
119-
if isinstance(all_data, Series):
120-
assert not (std_x < 0).any()
121-
else:
122-
assert not (std_x < 0).any().any()
110+
assert not (std_x < 0).any(axis=None)
123111

124112
# check that var(x) == std(x)^2
125113
tm.assert_equal(var_x, std_x * std_x)
126114

127115
cov_x_x = all_data.rolling(
128116
window=window, min_periods=min_periods, center=center
129117
).cov(all_data, ddof=ddof)
130-
if isinstance(all_data, Series):
131-
assert not (cov_x_x < 0).any()
132-
else:
133-
assert not (cov_x_x < 0).any().any()
118+
assert not (cov_x_x < 0).any(axis=None)
134119

135120
# check that var(x) == cov(x, x)
136121
tm.assert_equal(var_x, cov_x_x)

0 commit comments

Comments
 (0)