Catch Exception in combine (#22936) · pandas-dev/pandas@c19c805 (original) (raw)

``

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

`

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

`

`@@ -275,3 +271,47 @@ def test_compare_array(self, data, all_compare_operators):

`

275

271

`other = pd.Series(data) * [decimal.Decimal(pow(2.0, i))

`

276

272

`for i in alter]

`

277

273

`self._compare_other(s, data, op_name, other)

`

``

274

+

``

275

+

``

276

`+

class DecimalArrayWithoutFromSequence(DecimalArray):

`

``

277

`+

"""Helper class for testing error handling in _from_sequence."""

`

``

278

`+

def _from_sequence(cls, scalars, dtype=None, copy=False):

`

``

279

`+

raise KeyError("For the test")

`

``

280

+

``

281

+

``

282

`+

class DecimalArrayWithoutCoercion(DecimalArrayWithoutFromSequence):

`

``

283

`+

@classmethod

`

``

284

`+

def _create_arithmetic_method(cls, op):

`

``

285

`+

return cls._create_method(op, coerce_to_dtype=False)

`

``

286

+

``

287

+

``

288

`+

DecimalArrayWithoutCoercion._add_arithmetic_ops()

`

``

289

+

``

290

+

``

291

`+

def test_combine_from_sequence_raises():

`

``

292

`+

https://github.com/pandas-dev/pandas/issues/22850

`

``

293

`+

ser = pd.Series(DecimalArrayWithoutFromSequence([

`

``

294

`+

decimal.Decimal("1.0"),

`

``

295

`+

decimal.Decimal("2.0")

`

``

296

`+

]))

`

``

297

`+

result = ser.combine(ser, operator.add)

`

``

298

+

``

299

`+

note: object dtype

`

``

300

`+

expected = pd.Series([decimal.Decimal("2.0"),

`

``

301

`+

decimal.Decimal("4.0")], dtype="object")

`

``

302

`+

tm.assert_series_equal(result, expected)

`

``

303

+

``

304

+

``

305

`+

@pytest.mark.parametrize("class_", [DecimalArrayWithoutFromSequence,

`

``

306

`+

DecimalArrayWithoutCoercion])

`

``

307

`+

def test_scalar_ops_from_sequence_raises(class_):

`

``

308

`+

op(EA, EA) should return an EA, or an ndarray if it's not possible

`

``

309

`+

to return an EA with the return values.

`

``

310

`+

arr = class_([

`

``

311

`+

decimal.Decimal("1.0"),

`

``

312

`+

decimal.Decimal("2.0")

`

``

313

`+

])

`

``

314

`+

result = arr + arr

`

``

315

`+

expected = np.array([decimal.Decimal("2.0"), decimal.Decimal("4.0")],

`

``

316

`+

dtype="object")

`

``

317

`+

tm.assert_numpy_array_equal(result, expected)

`