Skip to content
Open
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
16 changes: 12 additions & 4 deletions awswrangler/_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from awswrangler import _arrow, exceptions
from awswrangler._distributed import engine

_PANDAS_DEFAULT_TIMESTAMP_UNIT = "us" if int(pd.__version__.split(".")[0]) >= 3 else "ns"
_logger: logging.Logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -341,7 +342,7 @@ def athena2pyarrow(dtype: str, df_type: str | None = None) -> pa.DataType: # no
elif df_type == "datetime64[s]":
return pa.timestamp(unit="s")
else:
return pa.timestamp(unit="ns")
return pa.timestamp(unit=_PANDAS_DEFAULT_TIMESTAMP_UNIT)
if dtype == "date":
return pa.date32()
if dtype in ("binary" or "varbinary"):
Expand Down Expand Up @@ -381,7 +382,11 @@ def athena2pandas(dtype: str, dtype_backend: str | None = None) -> str: # noqa:
if (dtype == "string") or dtype.startswith("char") or dtype.startswith("varchar"):
return "string" if dtype_backend != "pyarrow" else "string[pyarrow]"
if dtype in ("timestamp", "timestamp with time zone"):
return "datetime64" if dtype_backend != "pyarrow" else "timestamp[ns][pyarrow]"
return (
f"datetime64[{_PANDAS_DEFAULT_TIMESTAMP_UNIT}]"
if dtype_backend != "pyarrow"
else f"timestamp[{_PANDAS_DEFAULT_TIMESTAMP_UNIT}][pyarrow]"
)
if dtype == "date":
return "date" if dtype_backend != "pyarrow" else "date32[pyarrow]"
if dtype == "time":
Expand Down Expand Up @@ -607,6 +612,7 @@ def pyarrow2pandas_defaults(
"self_destruct": True,
"ignore_metadata": False,
"types_mapper": get_pyarrow2pandas_type_mapper(dtype_backend),
"coerce_temporal_nanoseconds": False,
}
if kwargs:
default_kwargs.update(kwargs)
Expand Down Expand Up @@ -766,8 +772,10 @@ def cast_pandas_with_athena_types(


def _normalize_pandas_dtype_name(dtype: str) -> str:
if dtype.startswith("datetime64[") is True:
return dtype # preserve datetime64[us], datetime64[ns], etc.
if dtype.startswith("datetime64") is True:
return "datetime64"
return "datetime64" # bare datetime64 without resolution stays as-is
if dtype.startswith("decimal") is True:
return "decimal"
return dtype
Expand All @@ -785,7 +793,7 @@ def _cast2date(value: Any) -> Any:

def _cast_pandas_column(df: pd.DataFrame, col: str, current_type: str, desired_type: str) -> pd.DataFrame:
if desired_type == "datetime64":
df[col] = pd.to_datetime(df[col])
df[col] = pd.to_datetime(df[col]).astype(f"datetime64[{_PANDAS_DEFAULT_TIMESTAMP_UNIT}]")
elif desired_type == "date":
df[col] = df[col].apply(lambda x: _cast2date(value=x)).replace(to_replace={pd.NaT: None})
elif desired_type == "bytes":
Expand Down
8 changes: 6 additions & 2 deletions awswrangler/s3/_write_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ def _new_writer(
if not pyarrow_additional_kwargs:
pyarrow_additional_kwargs = {}
if "coerce_timestamps" not in pyarrow_additional_kwargs:
pyarrow_additional_kwargs["coerce_timestamps"] = "ms"
pyarrow_additional_kwargs["coerce_timestamps"] = "us"
if "flavor" not in pyarrow_additional_kwargs:
pyarrow_additional_kwargs["flavor"] = "spark"
if "use_deprecated_int96_timestamps" not in pyarrow_additional_kwargs:
pyarrow_additional_kwargs["use_deprecated_int96_timestamps"] = False
if "version" not in pyarrow_additional_kwargs:
# By default, use version 1.0 logical type set to maximize compatibility
pyarrow_additional_kwargs["version"] = "1.0"
Expand Down Expand Up @@ -712,9 +714,11 @@ def to_parquet(
if not pyarrow_additional_kwargs:
pyarrow_additional_kwargs = {}
if "coerce_timestamps" not in pyarrow_additional_kwargs:
pyarrow_additional_kwargs["coerce_timestamps"] = "ms"
pyarrow_additional_kwargs["coerce_timestamps"] = "us"
if "flavor" not in pyarrow_additional_kwargs:
pyarrow_additional_kwargs["flavor"] = "spark"
if "use_deprecated_int96_timestamps" not in pyarrow_additional_kwargs:
pyarrow_additional_kwargs["use_deprecated_int96_timestamps"] = False

strategy = _S3ParquetWriteStrategy()
return strategy.write(
Expand Down