Skip to content

Commit 78082de

Browse files
committed
fix: use positional indexing to detect dates in pandas dataframes
Close #36
1 parent 053d502 commit 78082de

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎src/pyobsplot/data.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def pd_to_arrow(df: pd.DataFrame) -> bytes:
6262
# Convert dates to timestamps
6363
for colname in df.columns:
6464
col = df[colname].dropna()
65-
if col is not None and isinstance(col[0], date):
65+
if col is not None and len(col) > 0 and isinstance(col.iloc[0], date):
6666
try:
6767
df[colname] = pd.to_datetime(df[colname])
6868
except ValueError:

‎tests/test_data.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import io
66

7+
import numpy as np
78
import pandas as pd
89
import polars as pl
910
from polars.testing import assert_frame_equal
@@ -42,3 +43,12 @@ def test_data_frame_polars(self):
4243
pl.Float64,
4344
pl.Utf8,
4445
]
46+
47+
def test_pandas_indexing(self):
48+
df = pd.DataFrame({"x": [1, 2, 3, 4, 5], "y": [2, 1, 4, 3, 5]})
49+
filtered = df[df.x > 1]
50+
pd_to_arrow(filtered)
51+
52+
def test_pandas_dropna(self):
53+
df = pd.DataFrame({"x": [np.nan, np.nan, np.nan], "y": [2, 1, 4]})
54+
pd_to_arrow(df)

0 commit comments

Comments
 (0)