Skip to content

CLN: cleanup json pandas tests coverage of datetime units - #66759

Open
jorisvandenbossche wants to merge 2 commits into
pandas-dev:mainfrom
jorisvandenbossche:clean-tests-json-pandas-datetime
Open

CLN: cleanup json pandas tests coverage of datetime units#66759
jorisvandenbossche wants to merge 2 commits into
pandas-dev:mainfrom
jorisvandenbossche:clean-tests-json-pandas-datetime

Conversation

@jorisvandenbossche

@jorisvandenbossche jorisvandenbossche commented Aug 14, 2026

Copy link
Copy Markdown
Member

While updating those tests in the PR changing the default unit (#65845), I thought those tests could use a cleanup (it will allow to simplify the tests a bit more in that PR):

  • Change the standard test fixtures to use the default unit (now microseconds) instead of hardcoding to nanos, to ensure we are mostly testing with the standard unit (there are other tests that are parametrized on all possible units to have coverage for that)
  • Remove a bunch of other places where fixed / manually specified unit was not needed

@jorisvandenbossche jorisvandenbossche added this to the 3.1 milestone Aug 14, 2026
@jorisvandenbossche jorisvandenbossche added Testing pandas testing functions or related to the test suite IO JSON read_json, to_json, json_normalize Clean labels Aug 14, 2026
Comment on lines -550 to -552
index = date_range("20000101", periods=10, freq="h", unit="ns")
# freq doesn't round-trip
index = DatetimeIndex(list(index), freq=None)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is about a mixture of column dtypes, not need to also cover a datetime index (complicating the test further)

Comment on lines -1377 to +1354
result = read_json(StringIO(ser.to_json()), typ="series").apply(converter)
tm.assert_series_equal(result, ser.astype("m8[ms]"))
result = read_json(StringIO(ser.to_json()), typ="series")
expected = ser.dt.as_unit("ms").astype("int64")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating an expected, instead of postprocessing the result to match the original data (more logical test structure)

Comment on lines -1412 to +1395
result["a"] = pd.to_timedelta(result.a, unit="ns")
result["c"] = pd.to_datetime(result.c)
tm.assert_frame_equal(frame, result)
expected = frame.copy()
expected["a"] = expected["a"].dt.as_unit("ns").astype("int64")
expected["c"] = expected["c"].dt.as_unit("ns").astype("int64")
tm.assert_frame_equal(result, expected)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, creating an expected, instead of postprocessing the result to match the original data (more logical test structure)

assert result == expected

@pytest.mark.parametrize("as_object", [True, False])
@pytest.mark.parametrize("timedelta_typ", [pd.Timedelta, timedelta])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary param: regardless of those two input types, that gets inferred as a timedelta64 series/index anyway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Clean IO JSON read_json, to_json, json_normalize Testing pandas testing functions or related to the test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant