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
4 changes: 2 additions & 2 deletions python/pyarrow/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_extension_dtype_info(column):
physical_dtype = str(cats.codes.dtype)
elif hasattr(dtype, 'tz'):
metadata = {'timezone': pa.lib.tzinfo_to_string(dtype.tz)}
physical_dtype = 'datetime64[ns]'
physical_dtype = f'datetime64[{dtype.unit}]'
else:
metadata = None
physical_dtype = str(dtype)
Expand Down Expand Up @@ -1188,7 +1188,7 @@ def _reconstruct_columns_from_metadata(columns, column_indexes):
if _pandas_api.is_ge_v3():
# with pandas 3+, to_datetime returns a unit depending on the string
# data, so we restore it to the original unit from the metadata
level = level.as_unit(np.datetime_data(dtype)[0])
level = level.as_unit(np.datetime_data(numpy_dtype)[0])
# GH-41503: if the column index was decimal, restore to decimal
elif pandas_dtype == "decimal":
level = _pandas_api.pd.Index([decimal.Decimal(i) for i in level])
Expand Down
26 changes: 20 additions & 6 deletions python/pyarrow/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ def test_column_index_names_are_preserved(self):
df.columns.names = ['a']
_check_pandas_roundtrip(df, preserve_index=True)

def test_column_index_names_with_tz(self):
@pytest.mark.parametrize("tz", [None, "Europe/Brussels"])
def test_column_index_names_datetime(self, tz):
# ARROW-13756
# Bug if index is timezone aware DataTimeIndex

df = pd.DataFrame(
np.random.randn(5, 3),
columns=pd.date_range("2021-01-01", periods=3, freq="50D", tz="CET")
columns=pd.date_range("2021-01-01", periods=3, freq="50D", tz=tz)
)
_check_pandas_roundtrip(df, preserve_index=True)

Expand Down Expand Up @@ -447,11 +448,16 @@ def test_string_column_index(self):
assert len(md) == 1
assert md['encoding'] == 'UTF-8'

def test_datetimetz_column_index(self):
@pytest.mark.parametrize('unit', ['us', 'ns'])
def test_datetimetz_column_index(self, unit):
ext_kwargs = {}
if Version(pd.__version__) >= Version("2.0.0"):
# unit argument not supported on date_range for pandas < 2.0.0
ext_kwargs = {'unit': unit}
df = pd.DataFrame(
[(1, 'a', 2.0), (2, 'b', 3.0), (3, 'c', 4.0)],
columns=pd.date_range(
start='2017-01-01', periods=3, tz='America/New_York'
start='2017-01-01', periods=3, tz='America/New_York', **ext_kwargs
)
)
t = pa.Table.from_pandas(df, preserve_index=True)
Expand All @@ -460,7 +466,10 @@ def test_datetimetz_column_index(self):
column_indexes, = js['column_indexes']
assert column_indexes['name'] is None
assert column_indexes['pandas_type'] == 'datetimetz'
assert column_indexes['numpy_type'] == 'datetime64[ns]'
if ext_kwargs:
assert column_indexes['numpy_type'] == f'datetime64[{unit}]'
else:
assert column_indexes['numpy_type'] == 'datetime64[ns]'

md = column_indexes['metadata']
assert md['timezone'] == 'America/New_York'
Expand Down Expand Up @@ -709,7 +718,12 @@ def test_mismatch_metadata_schema(self):
# It is possible that the metadata and actual schema is not fully
# matching (eg no timezone information for tz-aware column)
# -> to_pandas() conversion should not fail on that
df = pd.DataFrame({"datetime": pd.date_range("2020-01-01", periods=3)})
ext_kwargs = {}
if Version(pd.__version__) >= Version("2.0.0"):
# unit argument not supported on date_range for pandas < 2.0.0
ext_kwargs = {'unit': 'ns'}
df = pd.DataFrame({"datetime": pd.date_range(
"2020-01-01", periods=3, **ext_kwargs)})

# OPTION 1: casting after conversion
table = pa.Table.from_pandas(df)
Expand Down
Loading