jorisvandenbossche commented on code in PR #14448:
URL: https://github.com/apache/arrow/pull/14448#discussion_r999144432


##########
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:
   `Py_BuildValue` is not meant to create a PyObject string in general like 
that.
   
   It seems to me that `PyObject_CallMethod(datetime_utc, "astimezone", "O", 
tzinfo.obj())` (what you had before) should work, though



-- 
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]

Reply via email to