[ 
https://issues.apache.org/jira/browse/ARROW-1730?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16220910#comment-16220910
 ] 

ASF GitHub Bot commented on ARROW-1730:
---------------------------------------

wesm commented on a change in pull request #1256: ARROW-1730, ARROW-1738: 
[Python] Fix wrong datetime conversion
URL: https://github.com/apache/arrow/pull/1256#discussion_r147220627
 
 

 ##########
 File path: cpp/src/arrow/python/builtin_convert.cc
 ##########
 @@ -522,18 +522,49 @@ class UInt64Converter : public 
TypedConverterVisitor<UInt64Builder, UInt64Conver
 class DateConverter : public TypedConverterVisitor<Date64Builder, 
DateConverter> {
  public:
   inline Status AppendItem(const OwnedRef& item) {
-    auto pydate = reinterpret_cast<PyDateTime_Date*>(item.obj());
-    return typed_builder_->Append(PyDate_to_ms(pydate));
+    int64_t t;
+    if (PyDate_Check(item.obj())) {
+      auto pydate = reinterpret_cast<PyDateTime_Date*>(item.obj());
+      t = PyDate_to_ms(pydate);
+    } else {
+      t = static_cast<int64_t>(PyLong_AsLongLong(item.obj()));
+    }
+    return typed_builder_->Append(t);
   }
 };
 
 class TimestampConverter
     : public TypedConverterVisitor<Date64Builder, TimestampConverter> {
  public:
+  TimestampConverter(TimeUnit::type unit) : unit_(unit) {}
+
   inline Status AppendItem(const OwnedRef& item) {
-    auto pydatetime = reinterpret_cast<PyDateTime_DateTime*>(item.obj());
-    return typed_builder_->Append(PyDateTime_to_us(pydatetime));
+    int64_t t;
+    if (PyDateTime_Check(item.obj())) {
+      auto pydatetime = reinterpret_cast<PyDateTime_DateTime*>(item.obj());
+
+      switch (unit_) {
+        case TimeUnit::SECOND:
+          t = PyDateTime_to_s(pydatetime);
+          break;
+        case TimeUnit::MILLI:
+          t = PyDateTime_to_ms(pydatetime);
+          break;
+        case TimeUnit::MICRO:
+          t = PyDateTime_to_us(pydatetime);
+          break;
+        case TimeUnit::NANO:
+          t = PyDateTime_to_ns(pydatetime);
+          break;
+      }
+    } else {
+      t = static_cast<int64_t>(PyLong_AsLongLong(item.obj()));
 
 Review comment:
   see above

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Python] Incorrect result from pyarrow.array when passing timestamp type
> ------------------------------------------------------------------------
>
>                 Key: ARROW-1730
>                 URL: https://issues.apache.org/jira/browse/ARROW-1730
>             Project: Apache Arrow
>          Issue Type: Bug
>          Components: Python
>            Reporter: Wes McKinney
>            Assignee: Licht Takeuchi
>              Labels: pull-request-available
>             Fix For: 0.8.0
>
>
> Even with the ARROW-1484 patch, we have:
> {code: language=python}
> In [10]: pa.array([0], type=pa.timestamp('ns'))
> Out[10]: 
> <pyarrow.lib.TimestampArray object at 0x7f9145b27098>
> [
>   Timestamp('1968-01-12 11:18:14.409378304')
> ]
> In [11]: pa.array([0], type='int64').cast(pa.timestamp('ns'))
> Out[11]: 
> <pyarrow.lib.TimestampArray object at 0x7f9145b27d18>
> [
>   Timestamp('1970-01-01 00:00:00')
> ]
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to