Skip to content

Commit

Permalink
Figure out Pandas version when version utils are not available
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Traverse committed Oct 31, 2023
1 parent 8fbcba5 commit 48ccf79
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions tracdap-runtime/python/src/tracdap/rt/_impl/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import decimal
import platform

import pandas.util.version
import pyarrow as pa
import pyarrow.compute as pc
import pandas as pd
Expand Down Expand Up @@ -108,14 +107,16 @@ class DataMapping:
}

# Check the Pandas dtypes for handling floats are available before setting up the type mapping
__PANDAS_VERSION = pd.util.version.parse(pd.__version__)
__PANDAS_VERSION_ELEMENTS = pd.__version__.split(".")
__PANDAS_VERSION_MAJOR = int(__PANDAS_VERSION_ELEMENTS[0])
__PANDAS_VERSION_MINOR = int(__PANDAS_VERSION_ELEMENTS[1])

if __PANDAS_VERSION.major == 2:
if __PANDAS_VERSION_MAJOR == 2:
__PANDAS_DATE_TYPE = pd.to_datetime([dt.date(2000, 1, 1)]).as_unit(__TRAC_TIMESTAMP_UNIT).dtype
__PANDAS_DATETIME_TYPE = pd.to_datetime([dt.datetime(2000, 1, 1, 0, 0, 0)]).as_unit(__TRAC_TIMESTAMP_UNIT).dtype

# pd.Float64Dtype was introduced in Pandas 1.2
elif __PANDAS_VERSION.major == 1 and __PANDAS_VERSION.minor >= 2:
elif __PANDAS_VERSION_MAJOR == 1 and __PANDAS_VERSION_MINOR >= 2:
__PANDAS_DATE_TYPE = pd.to_datetime([dt.date(2000, 1, 1)]).dtype
__PANDAS_DATETIME_TYPE = pd.to_datetime([dt.datetime(2000, 1, 1, 0, 0, 0)]).dtype

Expand Down Expand Up @@ -240,7 +241,7 @@ def pandas_datetime_type(cls, tz=None):
if tz is None:
return cls.__PANDAS_DATETIME_TYPE
else:
if cls.__PANDAS_VERSION.major == 1:
if cls.__PANDAS_VERSION_MAJOR == 1:
return pd.DatetimeTZDtype(tz=tz)
else:
return pd.DatetimeTZDtype(tz=tz, unit=cls.__TRAC_TIMESTAMP_UNIT)
Expand Down Expand Up @@ -401,8 +402,6 @@ class DataConformance:
"Field [{field_name}] cannot be converted from {vector_type} to {field_type}, " + \
"source and target have different time zones"

__PANDAS_MAJOR_VERSION = pd.util.version.parse(pd.__version__).major

@classmethod
def column_filter(cls, columns: tp.List[str], schema: tp.Optional[pa.Schema]) -> tp.Optional[tp.List[str]]:

Expand Down

0 comments on commit 48ccf79

Please sign in to comment.