|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +from datetime import datetime |
| 16 | + |
15 | 17 | import pandas as pd
|
16 | 18 | import pytest
|
| 19 | +import pytz |
17 | 20 |
|
18 | 21 | import bigframes.pandas as bpd
|
19 | 22 | from tests.system.utils import assert_pandas_df_equal
|
@@ -477,3 +480,62 @@ def test_qcut(scalars_dfs, q):
|
477 | 480 | pd_result = pd_result.astype("Int64")
|
478 | 481 |
|
479 | 482 | pd.testing.assert_series_equal(bf_result, pd_result)
|
| 483 | + |
| 484 | + |
| 485 | +@pytest.mark.parametrize( |
| 486 | + ("arg", "utc", "unit", "format"), |
| 487 | + [ |
| 488 | + (173872738, False, None, None), |
| 489 | + (32787983.23, True, "s", None), |
| 490 | + ("2023-01-01", False, None, "%Y-%m-%d"), |
| 491 | + (datetime(2023, 1, 1, 12, 0), False, None, None), |
| 492 | + ], |
| 493 | +) |
| 494 | +def test_to_datetime_scalar(arg, utc, unit, format): |
| 495 | + bf_result = bpd.to_datetime(arg, utc=utc, unit=unit, format=format) |
| 496 | + pd_result = pd.to_datetime(arg, utc=utc, unit=unit, format=format) |
| 497 | + |
| 498 | + assert bf_result == pd_result |
| 499 | + |
| 500 | + |
| 501 | +@pytest.mark.parametrize( |
| 502 | + ("arg", "utc", "unit", "format"), |
| 503 | + [ |
| 504 | + ([173872738], False, None, None), |
| 505 | + ([32787983.23], True, "s", None), |
| 506 | + ( |
| 507 | + [datetime(2023, 1, 1, 12, 0, tzinfo=pytz.timezone("America/New_York"))], |
| 508 | + True, |
| 509 | + None, |
| 510 | + None, |
| 511 | + ), |
| 512 | + (["2023-01-01"], True, None, "%Y-%m-%d"), |
| 513 | + (["2023-02-01T15:00:00+07:22"], True, None, None), |
| 514 | + (["01-31-2023 14:30 -0800"], True, None, "%m-%d-%Y %H:%M %z"), |
| 515 | + (["01-31-2023 14:00", "02-01-2023 15:00"], True, None, "%m-%d-%Y %H:%M"), |
| 516 | + ], |
| 517 | +) |
| 518 | +def test_to_datetime_iterable(arg, utc, unit, format): |
| 519 | + bf_result = ( |
| 520 | + bpd.to_datetime(arg, utc=utc, unit=unit, format=format) |
| 521 | + .to_pandas() |
| 522 | + .astype("datetime64[ns, UTC]" if utc else "datetime64[ns]") |
| 523 | + ) |
| 524 | + pd_result = pd.Series( |
| 525 | + pd.to_datetime(arg, utc=utc, unit=unit, format=format) |
| 526 | + ).dt.floor("us") |
| 527 | + pd.testing.assert_series_equal( |
| 528 | + bf_result, pd_result, check_index_type=False, check_names=False |
| 529 | + ) |
| 530 | + |
| 531 | + |
| 532 | +def test_to_datetime_series(scalars_dfs): |
| 533 | + scalars_df, scalars_pandas_df = scalars_dfs |
| 534 | + col = "int64_too" |
| 535 | + bf_result = ( |
| 536 | + bpd.to_datetime(scalars_df[col], unit="s").to_pandas().astype("datetime64[s]") |
| 537 | + ) |
| 538 | + pd_result = pd.Series(pd.to_datetime(scalars_pandas_df[col], unit="s")) |
| 539 | + pd.testing.assert_series_equal( |
| 540 | + bf_result, pd_result, check_index_type=False, check_names=False |
| 541 | + ) |
0 commit comments