You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/05/27 03:25:21 UTC

[GitHub] [arrow] jorisvandenbossche commented on a change in pull request #7258: ARROW-8918: [C++][Python] Implement cast metafunction to allow use of "cast" with CallFunction, use in Python

jorisvandenbossche commented on a change in pull request #7258:
URL: https://github.com/apache/arrow/pull/7258#discussion_r430274042



##########
File path: python/pyarrow/_compute.pyx
##########
@@ -316,3 +322,58 @@ cdef class FilterOptions(FunctionOptions):
 
     cdef const CFunctionOptions* options(self) except NULL:
         return &self.filter_options
+
+
+def cast(object arr, object target_type, bint safe=True):
+    """
+    Cast array values to another data type.
+
+    Example
+    -------
+
+    >>> from datetime import datetime
+    >>> import pyarrow as pa
+    >>> arr = pa.array([datetime(2010, 1, 1), datetime(2015, 1, 1)])
+    >>> arr.type
+    TimestampType(timestamp[us])
+
+    You can use ``pyarrow.DataType`` objects to specify the target type:
+
+    >>> arr.cast(pa.timestamp('ms'))
+    <pyarrow.lib.TimestampArray object at 0x10420eb88>
+    [
+      1262304000000,
+      1420070400000

Review comment:
       Minor note, but for me (on master), this gives actual string date representation instead of milliseconds:
   
   ```
   In [6]: arr.cast(pa.timestamp('ms'))
   Out[6]: 
   <pyarrow.lib.TimestampArray object at 0x7f95c16bcc48>
   [
     2010-01-01 00:00:00.000,
     2015-01-01 00:00:00.000
   ]
   ```

##########
File path: python/pyarrow/_compute.pyx
##########
@@ -316,3 +322,58 @@ cdef class FilterOptions(FunctionOptions):
 
     cdef const CFunctionOptions* options(self) except NULL:
         return &self.filter_options
+
+
+def cast(object arr, object target_type, bint safe=True):
+    """
+    Cast array values to another data type.
+
+    Example
+    -------

Review comment:
       ```suggestion
       Examples
       --------
   ```
   
   (typically the examples are also put after Parameters / Returns sections)

##########
File path: python/pyarrow/_compute.pyx
##########
@@ -316,3 +322,58 @@ cdef class FilterOptions(FunctionOptions):
 
     cdef const CFunctionOptions* options(self) except NULL:
         return &self.filter_options
+
+
+def cast(object arr, object target_type, bint safe=True):
+    """
+    Cast array values to another data type.
+
+    Example
+    -------
+
+    >>> from datetime import datetime
+    >>> import pyarrow as pa
+    >>> arr = pa.array([datetime(2010, 1, 1), datetime(2015, 1, 1)])
+    >>> arr.type
+    TimestampType(timestamp[us])
+
+    You can use ``pyarrow.DataType`` objects to specify the target type:
+
+    >>> arr.cast(pa.timestamp('ms'))
+    <pyarrow.lib.TimestampArray object at 0x10420eb88>
+    [
+      1262304000000,
+      1420070400000

Review comment:
       Ah, I see now it is just copied from the old docstrings below. That explains why the old repr is used here ;)




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

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