Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Fix OSError when displaying Polars DataFrames with old timestamps on Windows (#7477)

Myles Scolnick6280cd9f26ec
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
index f4b49f87b0..4c142a8f50 100644
--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py
+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
@@ -616,9 +616,36 @@ class NarwhalsTableManager(
int(hours), int(minutes), int(seconds), int(microseconds)
)
elif dtype == nw.Date:
- bin_end = datetime.date.fromtimestamp(bin_end / ms_time)
+ # Use timedelta to handle dates before Unix epoch (1970)
+ # which cause OSError on Windows with fromtimestamp
+ try:
+ bin_end = datetime.date.fromtimestamp(bin_end / ms_time)
+ except (OSError, OverflowError, ValueError):
+ # Fall back to timedelta calculation for old dates
+ epoch = datetime.datetime(
+ 1970, 1, 1, tzinfo=datetime.timezone.utc
+ )
+ bin_end_dt = epoch + datetime.timedelta(
+ seconds=bin_end / ms_time
+ )
+ bin_end = bin_end_dt.date()
else:
- bin_end = datetime.datetime.fromtimestamp(bin_end / ms_time)
+ # Use timedelta to handle datetimes before Unix epoch (1970)
+ # which cause OSError on Windows with fromtimestamp
+ try:
+ bin_end = datetime.datetime.fromtimestamp(
+ bin_end / ms_time
+ )
+ except (OSError, OverflowError, ValueError):
+ # Fall back to timedelta calculation for old dates
+ epoch = datetime.datetime(
+ 1970, 1, 1, tzinfo=datetime.timezone.utc
+ )
+ bin_end = epoch + datetime.timedelta(
+ seconds=bin_end / ms_time
+ )
+ # Remove timezone to match fromtimestamp behavior
+ bin_end = bin_end.replace(tzinfo=None)
# Only append if the count is greater than 0
if count > 0:
diff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.py
index e69e15bceb..6b3196f879 100644
--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py
+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py
@@ -1315,6 +1315,57 @@ class TestGetBinValuesTemporal:
assert bin_values[0].count == 5
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+ "df",
+ create_dataframes(
+ {
+ "old_dates": [
+ datetime.datetime.fromisoformat("1902-01-01 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-01 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-02 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-03 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-04 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-05 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-06 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-07 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-08 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-09 00:00:00"),
+ datetime.datetime.fromisoformat("2020-01-10 00:00:00"),
+ ]
+ },
+ exclude=["ibis"],
+ ),
+)
+class TestOldDates:
+ """Test handling of old dates (pre-1970) that cause OSError on Windows."""
+
+ def test_get_bin_values_with_old_dates(self, df: Any) -> None:
+ """
+ Test that get_bin_values works with timestamps before Unix epoch.
+
+ This reproduces issue #7469 where dates like 1902-01-01 cause
+ OSError: [Errno 22] Invalid argument on Windows when using
+ datetime.fromtimestamp().
+ """
+ manager = NarwhalsTableManager.from_dataframe(df)
+ # Should not raise OSError even with old dates
+ bin_values = manager.get_bin_values("old_dates", 3)
+
+ # Verify we get valid bin values
+ assert len(bin_values) > 0
+ assert all(
+ isinstance(bv.bin_start, (datetime.datetime, type(None)))
+ for bv in bin_values
+ )
+ assert all(
+ isinstance(bv.bin_end, (datetime.datetime, type(None)))
+ for bv in bin_values
+ )
+ # Total count should be 11 (all rows)
+ assert sum(bin_value.count for bin_value in bin_values) == 11
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
@pytest.mark.parametrize(
"df",