You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "HyukjinKwon (via GitHub)" <gi...@apache.org> on 2025/12/12 01:46:46 UTC

[PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

HyukjinKwon opened a new pull request, #48466:
URL: https://github.com/apache/arrow/pull/48466

   ### Rationale for this change
   
   https://github.com/apache/arrow/blob/0bfbd19bce3e10163537b349f9205b635c87eea7/python/pyarrow/src/arrow/python/arrow_to_pandas.cc#L1655-L1656
   
   This was first introduced in https://github.com/apache/arrow/commit/9b1b3979b499dc06b71a31b2696534550503d6e2 . For Python conversion path, we already truncate at https://github.com/apache/arrow/commit/9ca682bd9bc5c13fb1b99742c529e27629dcff7a so I believe we should make it consistent.
   
   ### What changes are included in this PR?
   
   This PR truncates truncate intraday milliseconds when converting Arrow instances to pandas instances.
   
   ### Are these changes tested?
   
   Yes, unittests were added, and tested as below:
   
   ```
   pytest pyarrow/tests/test_pandas.py
   ```
   
   ### Are there any user-facing changes?
   
   Yes. 
   
   ```python
   import numpy as np
   import pyarrow as pa
   import pandas as pd
   milliseconds_at_midnight = 1525910400000  # 2018-05-10 00:00:00
   milliseconds_with_time = 1525910400000 + 7425678  # + 2h 3m 45.678s
   arr = pa.array([milliseconds_at_midnight, milliseconds_with_time],
                  type=pa.date64())
   arr.to_pandas(date_as_object=False)
   ```
   
   Before:
   
   ```
   0   2018-05-10 00:00:00.000
   1   2018-05-10 02:03:45.678
   dtype: datetime64[ms]
   ```
   
   After:
   
   ```
   0   2018-05-10
   1   2018-05-10
   dtype: datetime64[ms]
   ```


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3737249500

   Can you rebase and resolve the conflicts?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3748490823

   @HyukjinKwon Thanks for the heads up, but please see this sentence also:
   > Date64 data that has a non-zero intraday component is invalid according to the Arrow format
   
   So it does not make sense to expose an option to truncate something that should already be truncated. I recommend closing this PR and the associated issues.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

Posted by "alippai (via GitHub)" <gi...@apache.org>.
alippai commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3655639779

   By spec `Date64` should be limited to full day values in arrow


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3668772032

   @alippai Thanks for reviewing this. I am fine with keeping the original behaviour as is, and add a switch. That is actually another todo for Python conversion at:
   
   https://github.com/apache/arrow/blob/d09233af103bbfcd3617820192d78fff44d5c73e/python/pyarrow/src/arrow/python/python_to_arrow.cc#L312
   
   If that's preferred, I can add a switch for Python and Arrow conversion sides, and keep the original behaviour as is (`True` for Python conv, and `False` for Arrow conv).
   
   Otherwise, we can also simply just remove this todo as well.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "EnricoMi (via GitHub)" <gi...@apache.org>.
EnricoMi commented on code in PR #48466:
URL: https://github.com/apache/arrow/pull/48466#discussion_r2664429167


##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1618,7 +1630,14 @@ class DatetimeMilliWriter : public DatetimeWriter<TimeUnit::MILLI> {
       // Convert from days since epoch to datetime64[ms]
       ConvertDatetime<int32_t, 86400000L>(*data, out_values);
     } else if (type == Type::DATE64) {
-      ConvertNumericNullable<int64_t>(*data, kPandasTimestampNull, out_values);
+      // Date64Type is millisecond timestamp
+      if (this->options_.truncate_date64_time) {
+        // Truncate intraday milliseconds
+        ConvertDatetimeWithTruncation<1L>(*data, out_values);

Review Comment:
   Can we avoid computing the `... * 1L` for each value in the array when `SHIFT == 1`? Or will the compiler optimize this away?



##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1548,6 +1547,19 @@ void ConvertDatesShift(const ChunkedArray& data, int64_t* out_values) {
   }
 }
 
+template <int64_t SHIFT>
+inline void ConvertDatetimeWithTruncation(const ChunkedArray& data, int64_t* out_values) {
+  for (int c = 0; c < data.num_chunks(); c++) {
+    const auto& arr = *data.chunk(c);
+    const int64_t* in_values = GetPrimitiveValues<int64_t>(arr);
+    for (int64_t i = 0; i < arr.length(); ++i) {
+      *out_values++ = arr.IsNull(i)
+                          ? kPandasTimestampNull
+                          : ((in_values[i] - in_values[i] % kMillisecondsInDay) * SHIFT);
+    }
+  }
+}

Review Comment:
   The `SHIFT` sounds like we are bit-shifting, where this is more a factor.
   
   ```suggestion
   template <int64_t FACTOR>
   inline void ConvertDatetimeWithTruncation(const ChunkedArray& data, int64_t* out_values) {
     for (int c = 0; c < data.num_chunks(); c++) {
       const auto& arr = *data.chunk(c);
       const int64_t* in_values = GetPrimitiveValues<int64_t>(arr);
       for (int64_t i = 0; i < arr.length(); ++i) {
         *out_values++ = arr.IsNull(i)
                             ? kPandasTimestampNull
                             : ((in_values[i] - in_values[i] % kMillisecondsInDay) * FACTOR);
       }
     }
   }
   ```



##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1548,6 +1547,19 @@ void ConvertDatesShift(const ChunkedArray& data, int64_t* out_values) {
   }
 }
 
+template <int64_t SHIFT>
+inline void ConvertDatetimeWithTruncation(const ChunkedArray& data, int64_t* out_values) {
+  for (int c = 0; c < data.num_chunks(); c++) {
+    const auto& arr = *data.chunk(c);
+    const int64_t* in_values = GetPrimitiveValues<int64_t>(arr);
+    for (int64_t i = 0; i < arr.length(); ++i) {
+      *out_values++ = arr.IsNull(i)
+                          ? kPandasTimestampNull
+                          : ((in_values[i] - in_values[i] % kMillisecondsInDay) * SHIFT);
+    }
+  }
+}

Review Comment:
   Looks like this naming exists in `ConvertDatetime` as well :-(.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3686218566

   Yeah let me work on 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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3695527517

   :warning: GitHub issue #48672 **has been automatically assigned in GitHub** to PR creator.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3725988325

   @AlenkaF do you mind taking a look when you find some time? I believe I resolved all comments. Now it does not change any default behaviour 🫡 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3746640835

   Yeah let me try to dedup and fix!
   
   @pitrou I meant to show the TLDR first and show the test, details and table generated by LLMs. That's the approach I usually use but seems like it was too verbose this time.
   
   So usually you won't have to read after (Generated by ChatGPT) part (and I am trying to mark it so to be honest there). Let me make it clear and concise next time.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "alippai (via GitHub)" <gi...@apache.org>.
alippai commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3695801122

   Looks good, thanks for the change


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3685983059

   I don't have a strong opinion on this either way. Avoiding a performance regression by making this non-default behavior seems like a good idea at this point.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3695744816

   This PR should be ready for a look.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3644546390

   :warning: GitHub issue #48465 **has been automatically assigned in GitHub** to PR creator.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

Posted by "alippai (via GitHub)" <gi...@apache.org>.
alippai commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3656400172

   Interesting, the `arr.to_pandas(date_as_object=False)` docs also says it should be the appropriate time unit (which is `D` in this case, not `ms`).
   
   Overall while I'm not a fan of introducing a slower conversion for managing a case violating the spec.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #48466:
URL: https://github.com/apache/arrow/pull/48466#discussion_r2666828836


##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1618,7 +1630,14 @@ class DatetimeMilliWriter : public DatetimeWriter<TimeUnit::MILLI> {
       // Convert from days since epoch to datetime64[ms]
       ConvertDatetime<int32_t, 86400000L>(*data, out_values);
     } else if (type == Type::DATE64) {
-      ConvertNumericNullable<int64_t>(*data, kPandasTimestampNull, out_values);
+      // Date64Type is millisecond timestamp
+      if (this->options_.truncate_date64_time) {
+        // Truncate intraday milliseconds
+        ConvertDatetimeWithTruncation<1L>(*data, out_values);

Review Comment:
   I believe it will optimize it out as a noop from my understanding but to make sure, I changed a bit to leverage `constexpr` for 1 case. It should compiletime branch it out, and should be optimized enough as documented in c++ lang.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3748500121

   For the record:
   ```python
   >>> dates = pa.array([0, 86400000, None], type=pa.int64()).view(pa.date64())
   >>> dates.validate(full=True)
   >>> dates = pa.array([0, 86400001, None], type=pa.int64()).view(pa.date64())
   >>> dates.validate(full=True)
   Traceback (most recent call last):
     Cell In[8], line 1
       dates.validate(full=True)
     File pyarrow/array.pxi:1851 in pyarrow.lib.Array.validate
     File pyarrow/error.pxi:92 in pyarrow.lib.check_status
   ArrowInvalid: date64[ms] 86400001 does not represent a whole number of days
   ```


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon closed pull request #48466: GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64
URL: https://github.com/apache/arrow/pull/48466


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3748734754

   Ah okie that works to me!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48465: [Python] Truncate intraday milliseconds in Date64 to pandas conversion [arrow]

Posted by "AlenkaF (via GitHub)" <gi...@apache.org>.
AlenkaF commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3685844585

   cc @rok pinging in case you have any opinions on this topic.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3695708347

   This PR should be ready for a look. 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on code in PR #48466:
URL: https://github.com/apache/arrow/pull/48466#discussion_r2666822346


##########
python/pyarrow/src/arrow/python/arrow_to_pandas.cc:
##########
@@ -1548,6 +1547,19 @@ void ConvertDatesShift(const ChunkedArray& data, int64_t* out_values) {
   }
 }
 
+template <int64_t SHIFT>
+inline void ConvertDatetimeWithTruncation(const ChunkedArray& data, int64_t* out_values) {
+  for (int c = 0; c < data.num_chunks(); c++) {
+    const auto& arr = *data.chunk(c);
+    const int64_t* in_values = GetPrimitiveValues<int64_t>(arr);
+    for (int64_t i = 0; i < arr.length(); ++i) {
+      *out_values++ = arr.IsNull(i)
+                          ? kPandasTimestampNull
+                          : ((in_values[i] - in_values[i] % kMillisecondsInDay) * SHIFT);
+    }
+  }
+}

Review Comment:
   Yeah .. let me just keep it consistent for now



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3744617588

   Date64 data that has a non-zero intraday component is invalid according to the Arrow format, so this entire feature is undesirable:
   https://github.com/apache/arrow/blob/d54a2051cf9020a0fdf50836420c38ad14787abb/format/Schema.fbs#L246-L254
   
   Unrelated, but @HyukjinKwon please write a proper PR description next time, instead of pasting multiple pages of ChatGPT-generated text.
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3748741915

   Ps: I'll be on vacation till 19th so responses might be delayed!


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-48672, GH-48465: [Python] Add an option for truncating intraday milliseconds in Date64 [arrow]

Posted by "rok (via GitHub)" <gi...@apache.org>.
rok commented on PR #48466:
URL: https://github.com/apache/arrow/pull/48466#issuecomment-3745122531

   Agreed with @AlenkaF here, `NdarrayToArrow` shouldn't have a truncation parameter. Also tests could be parameterized to and significantly less verbose.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org