You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by jo...@apache.org on 2022/10/27 07:56:04 UTC
[arrow] branch master updated: ARROW-16547: [Python] to_pandas fails with FixedOffset timezones when timestamp_as_object is used (#14448)
This is an automated email from the ASF dual-hosted git repository.
jorisvandenbossche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 31e8eebd70 ARROW-16547: [Python] to_pandas fails with FixedOffset timezones when timestamp_as_object is used (#14448)
31e8eebd70 is described below
commit 31e8eebd701d44cc5418152df11fb251a9428943
Author: Alenka Frim <Al...@users.noreply.github.com>
AuthorDate: Thu Oct 27 09:55:57 2022 +0200
ARROW-16547: [Python] to_pandas fails with FixedOffset timezones when timestamp_as_object is used (#14448)
This PR tries to substitute `fromutc` method in `ConvertTimezoneAware ` (arrow_to_pandas.cc) as it errors when `tzinfo` is an instance of `pytz` timezone. Basically does the change from:
```python
tzinfo.fromutc(dt)
```
to
```python
dt.replace(tzinfo=datetime.timezone.utc).astimezone(tzinfo)
```
but in C++.
Thank you @ milesgranger for the help!
Lead-authored-by: Alenka Frim <fr...@gmail.com>
Co-authored-by: Alenka Frim <Al...@users.noreply.github.com>
Co-authored-by: Joris Van den Bossche <jo...@gmail.com>
Signed-off-by: Joris Van den Bossche <jo...@gmail.com>
---
python/pyarrow/src/arrow/python/arrow_to_pandas.cc | 14 +++++++++++++-
python/pyarrow/tests/test_pandas.py | 12 ++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/python/pyarrow/src/arrow/python/arrow_to_pandas.cc b/python/pyarrow/src/arrow/python/arrow_to_pandas.cc
index 6e12bb1962..f3cee6c65e 100644
--- a/python/pyarrow/src/arrow/python/arrow_to_pandas.cc
+++ b/python/pyarrow/src/arrow/python/arrow_to_pandas.cc
@@ -1074,11 +1074,23 @@ struct ObjectWriterVisitor {
auto ConvertTimezoneAware = [&](typename Type::c_type value, PyObject** out) {
PyObject* naive_datetime;
RETURN_NOT_OK(ConvertTimezoneNaive(value, &naive_datetime));
+
// convert the timezone naive datetime object to timezone aware
- *out = PyObject_CallMethod(tzinfo.obj(), "fromutc", "O", naive_datetime);
+ // two step conversion of the datetime mimics Python's code:
+ // dt.replace(tzinfo=datetime.timezone.utc).astimezone(tzinfo)
+ // first step: replacing timezone with timezone.utc (replace method)
+ OwnedRef args(PyTuple_New(0));
+ OwnedRef keywords(PyDict_New());
+ PyDict_SetItemString(keywords.obj(), "tzinfo", PyDateTime_TimeZone_UTC);
+ OwnedRef naive_datetime_replace(PyObject_GetAttrString(naive_datetime, "replace"));
+ OwnedRef datetime_utc(PyObject_Call(naive_datetime_replace.obj(), args.obj(), keywords.obj()));
+ // second step: adjust the datetime to tzinfo timezone (astimezone method)
+ *out = PyObject_CallMethod(datetime_utc.obj(), "astimezone", "O", tzinfo.obj());
+
// the timezone naive object is no longer required
Py_DECREF(naive_datetime);
RETURN_IF_PYERROR();
+
return Status::OK();
};
diff --git a/python/pyarrow/tests/test_pandas.py b/python/pyarrow/tests/test_pandas.py
index f843904f12..22deb63306 100644
--- a/python/pyarrow/tests/test_pandas.py
+++ b/python/pyarrow/tests/test_pandas.py
@@ -4472,6 +4472,18 @@ def test_timestamp_as_object_non_nanosecond(resolution, tz, dt):
assert result[0] == expected
+def test_timestamp_as_object_fixed_offset():
+ # ARROW-16547 to_pandas with timestamp_as_object=True and FixedOffset
+ pytz = pytest.importorskip("pytz")
+ import datetime
+ timezone = pytz.FixedOffset(120)
+ dt = timezone.localize(datetime.datetime(2022, 5, 12, 16, 57))
+
+ table = pa.table({"timestamp_col": pa.array([dt])})
+ result = table.to_pandas(timestamp_as_object=True)
+ assert pa.table(result) == table
+
+
def test_threaded_pandas_import():
invoke_script("pandas_threaded_import.py")