AlenkaF commented on code in PR #14448:
URL: https://github.com/apache/arrow/pull/14448#discussion_r999087696
##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1074,11 +1074,26 @@ 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)
+ OwnedRef args_astimezone(PyTuple_New(1));
+ PyTuple_SetItem(args_astimezone.obj(), 0, tzinfo.obj());
+ OwnedRef astimezone(PyObject_GetAttrString(datetime_utc.obj(),
"astimezone"));
+ *out = PyObject_CallObject(astimezone.obj(), args_astimezone.obj());
Review Comment:
Ah, for `PyObject_CallMethodOneArg` I need to define `name` as a `PyObject`
also, but I get segmentation fault in Python when using:
```cpp
OwnedRef astimezone(Py_BuildValue("astimezone"));
*out = PyObject_CallMethodOneArg(datetime_utc.obj(), astimezone.obj(),
tzinfo.obj());
```
On a separate issue: I also get segmentation fault at the current state of
the PR for `test_timestamp_as_object_non_nanosecond()` when `tz` is not None.
Am looking into it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]