Skip to content

Commit e36fb90

Browse files
author
Rohan Jain
committed
improve test cases
1 parent c77c3f7 commit e36fb90

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

pandas/core/arrays/arrow/array.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ def floordiv_compat(
148148
has_remainder = pc.not_equal(pc.multiply(divided, right), left)
149149
result = pc.if_else(
150150
pc.and_(
151+
has_remainder,
151152
pc.less(
152153
pc.bit_wise_xor(left, right),
153154
pa.scalar(0, type=divided.type),
154155
),
155-
has_remainder,
156156
),
157157
# GH 55561: floordiv should round towards negative infinity.
158158
# pc.divide_checked for integral types rounds towards 0.
@@ -167,6 +167,8 @@ def floordiv_compat(
167167
# For 2 unsigned integer operands, integer division is
168168
# same as floor division.
169169
result = divided
170+
# Ensure compatibility with older versions of pandas where
171+
# int8 // int64 returned int8 rather than int64.
170172
result = result.cast(left.type)
171173
else:
172174
# Use divide instead of divide_checked to match numpy

pandas/tests/extension/test_arrow.py

+32-21
Original file line numberDiff line numberDiff line change
@@ -3246,60 +3246,71 @@ def test_arrow_floordiv_large_values():
32463246
tm.assert_series_equal(result, expected)
32473247

32483248

3249-
def test_arrow_floordiv_large_integral_result():
3249+
@pytest.mark.parametrize("dtype", ["int64[pyarrow]", "uint64[pyarrow]"])
3250+
def test_arrow_floordiv_large_integral_result(dtype):
32503251
# GH 56676
3251-
a = pd.Series([18014398509481983, -9223372036854775808], dtype="int64[pyarrow]")
3252+
a = pd.Series([18014398509481983], dtype=dtype)
32523253
result = a // 1
32533254
tm.assert_series_equal(result, a)
32543255

32553256

3256-
def test_arrow_floordiv_larger_divisor():
3257+
@pytest.mark.parametrize("pa_type", tm.SIGNED_INT_PYARROW_DTYPES)
3258+
def test_arrow_floordiv_larger_divisor(pa_type):
32573259
# GH 56676
3258-
a = pd.Series([-23], dtype="int64[pyarrow]")
3260+
dtype = ArrowDtype(pa_type)
3261+
a = pd.Series([-23], dtype=dtype)
32593262
result = a // 24
3260-
expected = pd.Series([-1], dtype="int64[pyarrow]")
3263+
expected = pd.Series([-1], dtype=dtype)
32613264
tm.assert_series_equal(result, expected)
32623265

32633266

3264-
def test_arrow_floordiv_integral_invalid():
3267+
@pytest.mark.parametrize("pa_type", tm.SIGNED_INT_PYARROW_DTYPES)
3268+
def test_arrow_floordiv_integral_invalid(pa_type):
32653269
# GH 56676
3266-
a = pd.Series([-9223372036854775808], dtype="int64[pyarrow]")
3267-
with pytest.raises(pa.lib.ArrowInvalid, match="overflow"):
3270+
min_value = np.iinfo(pa_type.to_pandas_dtype()).min
3271+
a = pd.Series([min_value], dtype=ArrowDtype(pa_type))
3272+
with pytest.raises(pa.lib.ArrowInvalid, match="overflow|not in range"):
32683273
a // -1
32693274
with pytest.raises(pa.lib.ArrowInvalid, match="divide by zero"):
32703275
a // 0
32713276

32723277

3273-
def test_arrow_floordiv_floating_0_divisor():
3278+
@pytest.mark.parametrize("dtype", tm.FLOAT_PYARROW_DTYPES_STR_REPR)
3279+
def test_arrow_floordiv_floating_0_divisor(dtype):
32743280
# GH 56676
3275-
a = pd.Series([2], dtype="double[pyarrow]")
3281+
a = pd.Series([2], dtype=dtype)
32763282
result = a // 0
3277-
expected = pd.Series([float("inf")], dtype="double[pyarrow]")
3283+
expected = pd.Series([float("inf")], dtype=dtype)
32783284
tm.assert_series_equal(result, expected)
32793285

32803286

3281-
def test_arrow_floordiv_no_overflow():
3287+
@pytest.mark.parametrize("pa_type", tm.ALL_INT_PYARROW_DTYPES)
3288+
def test_arrow_integral_floordiv_large_values(pa_type):
32823289
# GH 56676
3283-
a = pd.Series([9223372036854775808], dtype="uint64[pyarrow]")
3284-
b = pd.Series([1], dtype="uint64[pyarrow]")
3290+
max_value = np.iinfo(pa_type.to_pandas_dtype()).max
3291+
dtype = ArrowDtype(pa_type)
3292+
a = pd.Series([max_value], dtype=dtype)
3293+
b = pd.Series([1], dtype=dtype)
32853294
result = a // b
32863295
tm.assert_series_equal(result, a)
32873296

32883297

3289-
def test_arrow_true_division_large_divisor():
3298+
@pytest.mark.parametrize("dtype", ["int64[pyarrow]", "uint64[pyarrow]"])
3299+
def test_arrow_true_division_large_divisor(dtype):
32903300
# GH 56706
3291-
a = pd.Series([0], dtype="int64[pyarrow]")
3292-
b = pd.Series([18014398509481983], dtype="int64[pyarrow]")
3301+
a = pd.Series([0], dtype=dtype)
3302+
b = pd.Series([18014398509481983], dtype=dtype)
32933303
expected = pd.Series([0], dtype="float64[pyarrow]")
32943304
result = a / b
32953305
tm.assert_series_equal(result, expected)
32963306

32973307

3298-
def test_arrow_floor_division_large_divisor():
3308+
@pytest.mark.parametrize("dtype", ["int64[pyarrow]", "uint64[pyarrow]"])
3309+
def test_arrow_floor_division_large_divisor(dtype):
32993310
# GH 56706
3300-
a = pd.Series([0], dtype="int64[pyarrow]")
3301-
b = pd.Series([18014398509481983], dtype="int64[pyarrow]")
3302-
expected = pd.Series([0], dtype="int64[pyarrow]")
3311+
a = pd.Series([0], dtype=dtype)
3312+
b = pd.Series([18014398509481983], dtype=dtype)
3313+
expected = pd.Series([0], dtype=dtype)
33033314
result = a // b
33043315
tm.assert_series_equal(result, expected)
33053316

0 commit comments

Comments
 (0)