|
| 1 | +import operator |
1 | 2 | import decimal
|
2 | 3 |
|
3 |
| -import random |
4 | 4 | import numpy as np
|
5 | 5 | import pandas as pd
|
6 | 6 | import pandas.util.testing as tm
|
7 | 7 | import pytest
|
8 | 8 |
|
9 | 9 | from pandas.tests.extension import base
|
10 | 10 |
|
11 |
| -from .array import DecimalDtype, DecimalArray, to_decimal |
12 |
| - |
13 |
| - |
14 |
| -def make_data(): |
15 |
| - return [decimal.Decimal(random.random()) for _ in range(100)] |
| 11 | +from .array import DecimalDtype, DecimalArray, make_data |
16 | 12 |
|
17 | 13 |
|
18 | 14 | @pytest.fixture
|
@@ -294,3 +290,47 @@ def test_compare_array(self, data, all_compare_operators):
|
294 | 290 | other = pd.Series(data) * [decimal.Decimal(pow(2.0, i))
|
295 | 291 | for i in alter]
|
296 | 292 | self._compare_other(s, data, op_name, other)
|
| 293 | + |
| 294 | + |
| 295 | +class DecimalArrayWithoutFromSequence(DecimalArray): |
| 296 | + """Helper class for testing error handling in _from_sequence.""" |
| 297 | + def _from_sequence(cls, scalars, dtype=None, copy=False): |
| 298 | + raise KeyError("For the test") |
| 299 | + |
| 300 | + |
| 301 | +class DecimalArrayWithoutCoercion(DecimalArrayWithoutFromSequence): |
| 302 | + @classmethod |
| 303 | + def _create_arithmetic_method(cls, op): |
| 304 | + return cls._create_method(op, coerce_to_dtype=False) |
| 305 | + |
| 306 | + |
| 307 | +DecimalArrayWithoutCoercion._add_arithmetic_ops() |
| 308 | + |
| 309 | + |
| 310 | +def test_combine_from_sequence_raises(): |
| 311 | + # https://github.com/pandas-dev/pandas/issues/22850 |
| 312 | + ser = pd.Series(DecimalArrayWithoutFromSequence([ |
| 313 | + decimal.Decimal("1.0"), |
| 314 | + decimal.Decimal("2.0") |
| 315 | + ])) |
| 316 | + result = ser.combine(ser, operator.add) |
| 317 | + |
| 318 | + # note: object dtype |
| 319 | + expected = pd.Series([decimal.Decimal("2.0"), |
| 320 | + decimal.Decimal("4.0")], dtype="object") |
| 321 | + tm.assert_series_equal(result, expected) |
| 322 | + |
| 323 | + |
| 324 | +@pytest.mark.parametrize("class_", [DecimalArrayWithoutFromSequence, |
| 325 | + DecimalArrayWithoutCoercion]) |
| 326 | +def test_scalar_ops_from_sequence_raises(class_): |
| 327 | + # op(EA, EA) should return an EA, or an ndarray if it's not possible |
| 328 | + # to return an EA with the return values. |
| 329 | + arr = class_([ |
| 330 | + decimal.Decimal("1.0"), |
| 331 | + decimal.Decimal("2.0") |
| 332 | + ]) |
| 333 | + result = arr + arr |
| 334 | + expected = np.array([decimal.Decimal("2.0"), decimal.Decimal("4.0")], |
| 335 | + dtype="object") |
| 336 | + tm.assert_numpy_array_equal(result, expected) |
0 commit comments