|
| 1 | +from datetime import date, time, timedelta |
| 2 | +from decimal import Decimal |
1 | 3 | import importlib |
2 | 4 | import os |
3 | 5 |
|
|
8 | 10 | import pytest |
9 | 11 | from pytz import FixedOffset, utc |
10 | 12 |
|
11 | | -from pandas.compat import PY3 |
| 13 | +from pandas.compat import PY3, u |
12 | 14 | import pandas.util._test_decorators as td |
13 | 15 |
|
14 | 16 | import pandas as pd |
@@ -514,6 +516,84 @@ def any_numpy_dtype(request): |
514 | 516 | return request.param |
515 | 517 |
|
516 | 518 |
|
| 519 | +# categoricals are handled separately |
| 520 | +_any_skipna_inferred_dtype = [ |
| 521 | + ('string', ['a', np.nan, 'c']), |
| 522 | + ('unicode' if not PY3 else 'string', [u('a'), np.nan, u('c')]), |
| 523 | + ('bytes' if PY3 else 'string', [b'a', np.nan, b'c']), |
| 524 | + ('empty', [np.nan, np.nan, np.nan]), |
| 525 | + ('empty', []), |
| 526 | + ('mixed-integer', ['a', np.nan, 2]), |
| 527 | + ('mixed', ['a', np.nan, 2.0]), |
| 528 | + ('floating', [1.0, np.nan, 2.0]), |
| 529 | + ('integer', [1, np.nan, 2]), |
| 530 | + ('mixed-integer-float', [1, np.nan, 2.0]), |
| 531 | + ('decimal', [Decimal(1), np.nan, Decimal(2)]), |
| 532 | + ('boolean', [True, np.nan, False]), |
| 533 | + ('datetime64', [np.datetime64('2013-01-01'), np.nan, |
| 534 | + np.datetime64('2018-01-01')]), |
| 535 | + ('datetime', [pd.Timestamp('20130101'), np.nan, pd.Timestamp('20180101')]), |
| 536 | + ('date', [date(2013, 1, 1), np.nan, date(2018, 1, 1)]), |
| 537 | + # The following two dtypes are commented out due to GH 23554 |
| 538 | + # ('complex', [1 + 1j, np.nan, 2 + 2j]), |
| 539 | + # ('timedelta64', [np.timedelta64(1, 'D'), |
| 540 | + # np.nan, np.timedelta64(2, 'D')]), |
| 541 | + ('timedelta', [timedelta(1), np.nan, timedelta(2)]), |
| 542 | + ('time', [time(1), np.nan, time(2)]), |
| 543 | + ('period', [pd.Period(2013), pd.NaT, pd.Period(2018)]), |
| 544 | + ('interval', [pd.Interval(0, 1), np.nan, pd.Interval(0, 2)])] |
| 545 | +ids, _ = zip(*_any_skipna_inferred_dtype) # use inferred type as fixture-id |
| 546 | + |
| 547 | + |
| 548 | +@pytest.fixture(params=_any_skipna_inferred_dtype, ids=ids) |
| 549 | +def any_skipna_inferred_dtype(request): |
| 550 | + """ |
| 551 | + Fixture for all inferred dtypes from _libs.lib.infer_dtype |
| 552 | +
|
| 553 | + The covered (inferred) types are: |
| 554 | + * 'string' |
| 555 | + * 'unicode' (if PY2) |
| 556 | + * 'empty' |
| 557 | + * 'bytes' (if PY3) |
| 558 | + * 'mixed' |
| 559 | + * 'mixed-integer' |
| 560 | + * 'mixed-integer-float' |
| 561 | + * 'floating' |
| 562 | + * 'integer' |
| 563 | + * 'decimal' |
| 564 | + * 'boolean' |
| 565 | + * 'datetime64' |
| 566 | + * 'datetime' |
| 567 | + * 'date' |
| 568 | + * 'timedelta' |
| 569 | + * 'time' |
| 570 | + * 'period' |
| 571 | + * 'interval' |
| 572 | +
|
| 573 | + Returns |
| 574 | + ------- |
| 575 | + inferred_dtype : str |
| 576 | + The string for the inferred dtype from _libs.lib.infer_dtype |
| 577 | + values : np.ndarray |
| 578 | + An array of object dtype that will be inferred to have |
| 579 | + `inferred_dtype` |
| 580 | +
|
| 581 | + Examples |
| 582 | + -------- |
| 583 | + >>> import pandas._libs.lib as lib |
| 584 | + >>> |
| 585 | + >>> def test_something(any_skipna_inferred_dtype): |
| 586 | + ... inferred_dtype, values = any_skipna_inferred_dtype |
| 587 | + ... # will pass |
| 588 | + ... assert lib.infer_dtype(values, skipna=True) == inferred_dtype |
| 589 | + """ |
| 590 | + inferred_dtype, values = request.param |
| 591 | + values = np.array(values, dtype=object) # object dtype to avoid casting |
| 592 | + |
| 593 | + # correctness of inference tested in tests/dtypes/test_inference.py |
| 594 | + return inferred_dtype, values |
| 595 | + |
| 596 | + |
517 | 597 | @pytest.fixture |
518 | 598 | def mock(): |
519 | 599 | """ |
|
0 commit comments