Skip to content

Commit

Permalink
Fix test for older pandas versions
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed May 31, 2024
1 parent eff13d7 commit b775850
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions audformat/core/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ def _load_csv(self, path: str):
"""
schemes = self.db.schemes

# === DTYPES ===
# === Infer dtypes ===

# Collect pyarrow dtypes
# of the CSV file,
Expand Down Expand Up @@ -890,6 +890,7 @@ def _load_csv(self, path: str):
else:
object_columns.append(column_id)

# === Read CSV ===
schema = pa.schema(pyarrow_dtypes)
table = csv.read_csv(
path,
Expand All @@ -908,8 +909,14 @@ def _load_csv(self, path: str):
pa.string(): pd.StringDtype(),
}.get, # we have to provide a callable, not a dict
)

# === Adjust dtypes ===

# Adjust dtypes, that cannot be handled by pyarrow
for column in timedelta_columns:
# Older versions of pandas cannot convert None to timedelta
# df[column] = df[column].map(lambda x: pd.NA if x is None else x)
df[column] = df[column].fillna(pd.NA)
df[column] = df[column].astype("timedelta64[ns]")
for column in boolean_columns:
df[column] = df[column].astype("boolean")
Expand All @@ -928,8 +935,8 @@ def _load_csv(self, path: str):
)
df[column] = df[column].astype(dtype)

# Set index
#
# === Set index ===

# When assigning more than one column,
# a MultiIndex is assigned.
# Setting a MultiIndex does not always preserve pandas dtypes,
Expand Down

0 comments on commit b775850

Please sign in to comment.