Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/datacustomcode/io/reader/query_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Union,
)

import pandas.api.types as pd_types
from pyspark.sql.types import (
BooleanType,
DoubleType,
Expand Down Expand Up @@ -48,8 +49,6 @@
"object": StringType(),
"int64": LongType(),
"float64": DoubleType(),
"datetime64[ns]": TimestampType(),
"datetime64[ns, UTC]": TimestampType(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could've added datetime64[ms, UTC] here, but I liked the is_datetime64_any_dtype approach below better to catch any arg combinations we might be missing in the future.

"bool": BooleanType(),
}

Expand All @@ -59,7 +58,11 @@ def _pandas_to_spark_schema(
) -> StructType:
fields = []
for column, dtype in pandas_df.dtypes.items():
spark_type = PANDAS_TYPE_MAPPING.get(str(dtype), StringType())
spark_type: AtomicType
if pd_types.is_datetime64_any_dtype(dtype):
spark_type = TimestampType()
else:
spark_type = PANDAS_TYPE_MAPPING.get(str(dtype), StringType())
fields.append(StructField(column, spark_type, nullable))
return StructType(fields)

Expand Down
50 changes: 50 additions & 0 deletions tests/io/reader/test_query_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
StringType,
StructField,
StructType,
TimestampType,
)
import pytest

Expand Down Expand Up @@ -59,6 +60,55 @@ def test_pandas_to_spark_schema_nullable(self):
schema = _pandas_to_spark_schema(df, nullable=False)
assert not schema.fields[0].nullable

def test_pandas_to_spark_schema_datetime_types(self):
"""Test conversion of pandas datetime types to Spark TimestampType."""

# Create test data with different datetime types
data = {
"datetime_ns": pd.to_datetime(
["2023-01-01 10:00:00", "2023-01-02 11:00:00"]
),
"datetime_ns_utc": pd.to_datetime(
["2023-01-01 10:00:00", "2023-01-02 11:00:00"], utc=True
),
"datetime_ms": pd.to_datetime(
["2023-01-01 10:00:00", "2023-01-02 11:00:00"]
).astype("datetime64[ms]"),
"datetime_ms_utc": pd.to_datetime(
["2023-01-01 10:00:00", "2023-01-02 11:00:00"], utc=True
)
.tz_localize(None)
.astype("datetime64[ms]"),
}
df = pd.DataFrame(data)

# Convert to Spark schema
schema = _pandas_to_spark_schema(df)

# Verify the schema
assert isinstance(schema, StructType)
assert len(schema.fields) == 4

# Check that all datetime columns map to TimestampType
field_dict = {field.name: field for field in schema.fields}
for field_name in [
"datetime_ns",
"datetime_ns_utc",
"datetime_ms",
"datetime_ms_utc",
]:
assert isinstance(field_dict[field_name].dataType, TimestampType), (
f"Field {field_name} should be TimestampType, "
f"got {type(field_dict[field_name].dataType)}"
)
assert field_dict[field_name].nullable

# Verify the actual pandas dtypes to ensure our test data has the expected types
assert str(df["datetime_ns"].dtype) == "datetime64[ns]"
assert str(df["datetime_ns_utc"].dtype) == "datetime64[ns, UTC]"
assert str(df["datetime_ms"].dtype) == "datetime64[ms]"
assert str(df["datetime_ms_utc"].dtype) == "datetime64[ms]"


# Completely isolated test class for QueryAPIDataCloudReader
@pytest.mark.usefixtures("patch_all_requests")
Expand Down