|
15 | 15 | import datetime
|
16 | 16 |
|
17 | 17 | import packaging.version
|
| 18 | +import pyarrow.lib |
18 | 19 | import pytest
|
19 | 20 |
|
20 | 21 | pd = pytest.importorskip("pandas")
|
@@ -171,18 +172,14 @@ def test_timearray_comparisons(
|
171 | 172 |
|
172 | 173 | # Bad shape
|
173 | 174 | for bad_shape in ([], [1, 2, 3]):
|
174 |
| - if op == "==": |
175 |
| - assert not comparisons[op](left, np.array(bad_shape)) |
176 |
| - assert complements[op](left, np.array(bad_shape)) |
177 |
| - else: |
178 |
| - with pytest.raises( |
179 |
| - ValueError, match="operands could not be broadcast together", |
180 |
| - ): |
181 |
| - comparisons[op](left, np.array(bad_shape)) |
182 |
| - with pytest.raises( |
183 |
| - ValueError, match="operands could not be broadcast together", |
184 |
| - ): |
185 |
| - complements[op](left, np.array(bad_shape)) |
| 175 | + with pytest.raises( |
| 176 | + TypeError, match="Can't compare arrays with different shapes" |
| 177 | + ): |
| 178 | + comparisons[op](left, np.array(bad_shape)) |
| 179 | + with pytest.raises( |
| 180 | + TypeError, match="Can't compare arrays with different shapes" |
| 181 | + ): |
| 182 | + complements[op](left, np.array(bad_shape)) |
186 | 183 |
|
187 | 184 | # Bad items
|
188 | 185 | for bad_items in (
|
@@ -478,8 +475,10 @@ def test_asdatetime(dtype, same):
|
478 | 475 | )
|
479 | 476 | def test_astimedelta(dtype):
|
480 | 477 | t = "01:02:03.123456"
|
481 |
| - expect = pd.to_timedelta([t]).array.astype( |
482 |
| - "timedelta64[ns]" if dtype == "timedelta" else dtype |
| 478 | + expect = ( |
| 479 | + pd.to_timedelta([t]) |
| 480 | + .to_numpy() |
| 481 | + .astype("timedelta64[ns]" if dtype == "timedelta" else dtype) |
483 | 482 | )
|
484 | 483 |
|
485 | 484 | a = _cls("time")([t, None])
|
@@ -543,7 +542,10 @@ def test_min_max_median(dtype):
|
543 | 542 | assert empty.min(skipna=False) is None
|
544 | 543 | assert empty.max(skipna=False) is None
|
545 | 544 | if pandas_release >= (1, 2):
|
546 |
| - assert empty.median() is None |
| 545 | + with pytest.warns(RuntimeWarning, match="empty slice"): |
| 546 | + # It's weird that we get the warning here, and not |
| 547 | + # below. :/ |
| 548 | + assert empty.median() is None |
547 | 549 | assert empty.median(skipna=False) is None
|
548 | 550 |
|
549 | 551 | a = _make_one(dtype)
|
@@ -620,3 +622,61 @@ def test_date_sub():
|
620 | 622 | do = pd.Series([pd.DateOffset(days=i) for i in range(4)])
|
621 | 623 | expect = dates.astype("object") - do
|
622 | 624 | assert np.array_equal(dates - do, expect)
|
| 625 | + |
| 626 | + |
| 627 | +@pytest.mark.parametrize( |
| 628 | + "value, expected", [("1", datetime.time(1)), ("1:2", datetime.time(1, 2))], |
| 629 | +) |
| 630 | +def test_short_time_parsing(value, expected): |
| 631 | + assert _cls("time")([value])[0] == expected |
| 632 | + |
| 633 | + |
| 634 | +@pytest.mark.parametrize( |
| 635 | + "value, error", |
| 636 | + [ |
| 637 | + ("thursday", "Bad time string: 'thursday'"), |
| 638 | + ("1:2:3thursday", "Bad time string: '1:2:3thursday'"), |
| 639 | + ("1:2:3:4", "Bad time string: '1:2:3:4'"), |
| 640 | + ("1:2:3.f", "Bad time string: '1:2:3.f'"), |
| 641 | + ("1:d:3", "Bad time string: '1:d:3'"), |
| 642 | + ("1:2.3", "Bad time string: '1:2.3'"), |
| 643 | + ("", "Bad time string: ''"), |
| 644 | + ("1:2:99", "second must be in 0[.][.]59"), |
| 645 | + ("1:99", "minute must be in 0[.][.]59"), |
| 646 | + ("99", "hour must be in 0[.][.]23"), |
| 647 | + ], |
| 648 | +) |
| 649 | +def test_bad_time_parsing(value, error): |
| 650 | + with pytest.raises(ValueError, match=error): |
| 651 | + _cls("time")([value]) |
| 652 | + |
| 653 | + |
| 654 | +@pytest.mark.parametrize( |
| 655 | + "value, error", |
| 656 | + [ |
| 657 | + ("thursday", "Bad date string: 'thursday'"), |
| 658 | + ("1-2-thursday", "Bad date string: '1-2-thursday'"), |
| 659 | + ("1-2-3-4", "Bad date string: '1-2-3-4'"), |
| 660 | + ("1-2-3.f", "Bad date string: '1-2-3.f'"), |
| 661 | + ("1-d-3", "Bad date string: '1-d-3'"), |
| 662 | + ("1-3", "Bad date string: '1-3'"), |
| 663 | + ("1", "Bad date string: '1'"), |
| 664 | + ("", "Bad date string: ''"), |
| 665 | + ("2021-2-99", "day is out of range for month"), |
| 666 | + ("2021-99-1", "month must be in 1[.][.]12"), |
| 667 | + ("10000-1-1", "year 10000 is out of range"), |
| 668 | + ], |
| 669 | +) |
| 670 | +def test_bad_date_parsing(value, error): |
| 671 | + with pytest.raises(ValueError, match=error): |
| 672 | + _cls("date")([value]) |
| 673 | + |
| 674 | + |
| 675 | +@for_date_and_time |
| 676 | +def test_date___arrow__array__(dtype): |
| 677 | + a = _make_one(dtype) |
| 678 | + ar = a.__arrow_array__() |
| 679 | + assert isinstance( |
| 680 | + ar, pyarrow.Date32Array if dtype == "date" else pyarrow.Time64Array, |
| 681 | + ) |
| 682 | + assert [v.as_py() for v in ar] == list(a) |
0 commit comments