Skip to content

Commit

Permalink
Fix duration typo, add is_v1() api to pandas shim
Browse files Browse the repository at this point in the history
  • Loading branch information
danepitkin committed Jun 5, 2023
1 parent 3131218 commit f0f8cba
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
6 changes: 2 additions & 4 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1682,10 +1682,8 @@ cdef _array_like_to_pandas(obj, options, types_mapper):
original_type = obj.type
name = obj._name

# ARROW-33321 reenables support for date/timestamp conversion in pandas >= 2.0
from pyarrow.vendored.version import Version
if pandas_api.loose_version < Version('2.0.0'):
# ARROW-3789(wesm): Convert date/timestamp types to datetime64[ns]
if pandas_api.is_v1():
# ARROW-3789: Convert date/timestamp types to datetime64[ns]
c_options.coerce_temporal_nanoseconds = True

if isinstance(obj, Array):
Expand Down
7 changes: 7 additions & 0 deletions python/pyarrow/pandas-shim.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ cdef class _PandasAPIShim(object):
self._pd = pd
self._version = pd.__version__
self._loose_version = Version(pd.__version__)
self._is_v1 = False

if self._loose_version < Version('1.0.0'):
self._have_pandas = False
Expand All @@ -72,6 +73,8 @@ cdef class _PandasAPIShim(object):
"installed. Therefore, pandas-specific integration is not "
"used.".format(self._version), stacklevel=2)
return
elif self._loose_version < Version('2.0.0'):
self._is_v1 = True

self._compat_module = pdcompat
self._data_frame = pd.DataFrame
Expand Down Expand Up @@ -150,6 +153,10 @@ cdef class _PandasAPIShim(object):
self._check_import()
return self._version

def is_v1(self):
self._check_import()
return self._is_v1

@property
def categorical_type(self):
self._check_import()
Expand Down
6 changes: 2 additions & 4 deletions python/pyarrow/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,10 +787,8 @@ def _reconstruct_block(item, columns=None, extension_columns=None):


def make_datetimetz(unit, tz):
from pyarrow.vendored.version import Version
# ARROW-3789: Convert date/timestamp types to datetime64[ns]
if _pandas_api.loose_version < Version('2.0.0'):
unit = 'ns'
if _pandas_api.is_v1():
unit = 'ns' # ARROW-3789: Convert date/timestamp types to datetime64[ns]
tz = pa.lib.string_to_tzinfo(tz)
return _pandas_api.datetimetz_type(unit, tz=tz)

Expand Down
6 changes: 2 additions & 4 deletions python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2892,10 +2892,8 @@ def table_to_blocks(options, Table table, categories, extension_columns):
c_options.extension_columns = {tobytes(col)
for col in extension_columns}

# ARROW-33321 reenables support for date/timestamp conversion in pandas >= 2.0
from pyarrow.vendored.version import Version
if pandas_api.loose_version < Version('2.0.0'):
# ARROW-3789(wesm); Convert date/timestamp types to datetime64[ns]
if pandas_api.is_v1():
# ARROW-3789: Convert date/timestamp types to datetime64[ns]
c_options.coerce_temporal_nanoseconds = True

if c_options.self_destruct:
Expand Down
10 changes: 5 additions & 5 deletions python/pyarrow/types.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ cdef dict _pandas_type_map = {
_Type_HALF_FLOAT: np.float16,
_Type_FLOAT: np.float32,
_Type_DOUBLE: np.float64,
_Type_DATE32: np.dtype('datetime64[D]'),
# Pandas does not support [D]ay, so default to [s]econd for date32
_Type_DATE32: np.dtype('datetime64[s]'),
_Type_DATE64: np.dtype('datetime64[ms]'),
_Type_TIMESTAMP: {
's': np.dtype('datetime64[s]'),
Expand Down Expand Up @@ -131,9 +132,8 @@ def _get_pandas_type(type, unit=None):
if type not in _pandas_type_map:
return None
if type in [_Type_DATE32, _Type_DATE64, _Type_TIMESTAMP, _Type_DURATION]:
from pyarrow.vendored.version import Version
# ARROW-3789: Convert date/timestamp types to datetime64[ns]
if _pandas_api.loose_version < Version('2.0.0'):
if _pandas_api.is_v1():
# ARROW-3789: Convert date/timestamp types to datetime64[ns]
if type == _Type_DURATION:
return np.dtype('timedelta64[ns]')
return np.dtype('datetime64[ns]')
Expand Down Expand Up @@ -1161,7 +1161,7 @@ cdef class DurationType(DataType):
>>> d.to_pandas_dtype()
timedelta64[ms]
"""
return _get_pandas_type(_Type_TIMESTAMP, self.unit)
return _get_pandas_type(_Type_DURATION, self.unit)


cdef class FixedSizeBinaryType(DataType):
Expand Down

0 comments on commit f0f8cba

Please sign in to comment.