tadeja commented on code in PR #55811:
URL: https://github.com/apache/arrow/pull/55811#discussion_r4769022291
##########
python/pyarrow/scalar.pxi:
##########
@@ -904,6 +904,92 @@ cdef class DurationScalar(Scalar):
return datetime.timedelta(microseconds=sp.value // 1000)
+cdef class MonthIntervalScalar(Scalar):
+ """
+ Concrete class for month interval scalars.
+ """
+
+ @property
+ def value(self):
+ """
+ Same as self.as_py()
+ """
+ return self.as_py()
+
+ def __repr__(self):
+ return '<pyarrow.{}: {}>'.format(
+ self.__class__.__name__, frombytes(self.wrapped.get().ToString())
+ )
+
+ def __str__(self):
+ return frombytes(self.wrapped.get().ToString())
+
+ def as_py(self, *, maps_as_pydicts=None):
Review Comment:
```suggestion
def as_py(self, *, maps_as_pydicts=None):
```
##########
python/pyarrow/scalar.pxi:
##########
@@ -904,6 +904,92 @@ cdef class DurationScalar(Scalar):
return datetime.timedelta(microseconds=sp.value // 1000)
+cdef class MonthIntervalScalar(Scalar):
+ """
+ Concrete class for month interval scalars.
+ """
+
+ @property
+ def value(self):
+ """
+ Same as self.as_py()
+ """
+ return self.as_py()
+
+ def __repr__(self):
+ return '<pyarrow.{}: {}>'.format(
+ self.__class__.__name__, frombytes(self.wrapped.get().ToString())
+ )
+
+ def __str__(self):
+ return frombytes(self.wrapped.get().ToString())
+
+ def as_py(self, *, maps_as_pydicts=None):
+ """
+ Return this value as a pyarrow.Month.
+
+ Parameters
+ ----------
+ maps_as_pydicts : str, optional, default `None`
+ Valid values are `None`, 'lossy', or 'strict'.
+ This parameter is ignored for non-nested Scalars.
+
+ Returns
+ -------
+ pyarrow.Month
+ """
+ cdef:
+ PyObject* val
+ CMonthIntervalScalar* scalar
+ scalar = <CMonthIntervalScalar*>self.wrapped.get()
+ val = GetResultValue(MonthIntervalScalarToPyObject(
+ deref(scalar)))
+ return PyObject_to_object(val)
+
+
+cdef class DayTimeIntervalScalar(Scalar):
+ """
+ Concrete class for day-time interval scalars.
+ """
+
+ @property
+ def value(self):
+ """
+ Same as self.as_py()
+ """
+ return self.as_py()
+
+ def __repr__(self):
+ return '<pyarrow.{}: {}>'.format(
+ self.__class__.__name__, frombytes(self.wrapped.get().ToString())
+ )
+
+ def __str__(self):
+ return frombytes(self.wrapped.get().ToString())
+
+ def as_py(self, *, maps_as_pydicts=None):
Review Comment:
```suggestion
def as_py(self, *, maps_as_pydicts=None):
```
These `__repr__` and `__str__` from @jorisvandenbossche's original
05c392ed4c could be removed now that @AlenkaF implemented `as_py()` for both
classes. So inherited `Scalar.__repr__` and `Scalar.__str__` work directly and
match `MonthDayNanoIntervalScalar`
Then doctests could be updated to match the new output ( like `4M ->
Month(months=4)` etc below in comments ) and null scalars will be `null ->
None`.
##########
python/pyarrow/tests/test_scalars.py:
##########
@@ -529,6 +529,34 @@ def test_duration_nanos_nopandas():
arr[0].as_py()
+def test_day_time_and_month_interval():
+ arr1 = pa.array(
+ [datetime.datetime(2020, 1, 1), None, datetime.datetime(1900, 12, 13)],
+ type=pa.timestamp('s')
+ )
+ arr2 = pa.array(
+ [datetime.datetime(2020, 5, 5, 1, 30), None, None],
+ type=pa.timestamp('s')
+ )
+
+ arr = pc.month_interval_between(arr1, arr2)
+ assert isinstance(arr[0].as_py(), pa.Month)
+ assert arr[0].as_py() == pa.Month([4])
+ assert arr[0].as_py().months == 4
+ assert arr[0].value == arr[0].as_py()
+ assert arr[1].as_py(maps_as_pydicts="strict") is None
+ assert arr[1].value is None
+
Review Comment:
```suggestion
assert arr[1].value is None
assert repr(arr[1]) == "<pyarrow.MonthIntervalScalar: None>"
```
##########
python/pyarrow/tests/test_compute.py:
##########
@@ -4631,3 +4631,32 @@ def test_winsorize():
result = pc.winsorize(
arr, options=pc.WinsorizeOptions(lower_limit=0.1, upper_limit=0.8))
assert result.to_pylist() == [8, 4, 8, 8, 5, 3, 7, 2, 2, 6]
+
+
+def test_interval_between():
+ arr1 = pa.array(
+ [datetime.datetime(2020, 1, 1), None, datetime.datetime(1900, 12, 13)],
+ type=pa.timestamp('s')
+ )
+ arr2 = pa.array(
+ [datetime.datetime(2020, 5, 5), datetime.datetime(2020, 10, 1),
+ datetime.datetime(2020, 1, 1)],
+ type=pa.timestamp('s')
+ )
+ result = pc.month_day_nano_interval_between(arr1, arr2)
+ assert isinstance(result, pa.MonthDayNanoIntervalArray)
+ assert isinstance(result[0], pa.MonthDayNanoIntervalScalar)
+
+ result = pc.month_interval_between(arr1, arr2)
+ assert isinstance(result, pa.MonthIntervalArray)
+ assert isinstance(result[0], pa.MonthIntervalScalar)
+ assert str(result[0]) == "4M"
Review Comment:
```suggestion
assert str(result[0]) == "Month(months=4)"
```
##########
python/pyarrow/types.pxi:
##########
@@ -4350,6 +4350,81 @@ def duration(unit):
return out
+def month_interval():
+ """
+ Create instance of an interval type representing months
+ between two dates.
+
+ Examples
+ --------
+ Create an instance of an month_interval type:
+
+ >>> import pyarrow as pa
+ >>> pa.month_interval()
+ DataType(month_interval)
+
+ The type can't be created from Python values yet, but
+ compute functions return it:
+
+ >>> import pyarrow as pa
+ >>> import pyarrow.compute as pc
+ >>> import datetime
+ >>> arr1 = pa.array([datetime.datetime(2020, 1, 1)],
type=pa.timestamp('s'))
+ >>> arr2 = pa.array([datetime.datetime(2020, 5, 5)],
type=pa.timestamp('s'))
+
+ Create an array with month_interval type:
+
+ >>> pc.month_interval_between(arr1, arr2)
+ <pyarrow.lib.MonthIntervalArray object at ...>
+ [
+ 4M
+ ]
+
+ or select a scalar with the same type:
+
+ >>> pc.month_interval_between(arr1, arr2)[0]
+ <pyarrow.MonthIntervalScalar: 4M>
+ """
Review Comment:
```suggestion
<pyarrow.MonthIntervalScalar: Month(months=4)>
"""
```
##########
python/pyarrow/tests/test_compute.py:
##########
@@ -4631,3 +4631,32 @@ def test_winsorize():
result = pc.winsorize(
arr, options=pc.WinsorizeOptions(lower_limit=0.1, upper_limit=0.8))
assert result.to_pylist() == [8, 4, 8, 8, 5, 3, 7, 2, 2, 6]
+
+
+def test_interval_between():
+ arr1 = pa.array(
+ [datetime.datetime(2020, 1, 1), None, datetime.datetime(1900, 12, 13)],
+ type=pa.timestamp('s')
+ )
+ arr2 = pa.array(
+ [datetime.datetime(2020, 5, 5), datetime.datetime(2020, 10, 1),
+ datetime.datetime(2020, 1, 1)],
+ type=pa.timestamp('s')
+ )
+ result = pc.month_day_nano_interval_between(arr1, arr2)
+ assert isinstance(result, pa.MonthDayNanoIntervalArray)
+ assert isinstance(result[0], pa.MonthDayNanoIntervalScalar)
+
+ result = pc.month_interval_between(arr1, arr2)
+ assert isinstance(result, pa.MonthIntervalArray)
+ assert isinstance(result[0], pa.MonthIntervalScalar)
+ assert str(result[0]) == "4M"
+ assert result.to_pylist() == [pa.Month([4]), None, pa.Month([1429])]
+
+ result = pc.day_time_interval_between(arr1, arr2)
+ assert isinstance(result, pa.DayTimeIntervalArray)
+ assert isinstance(result[0], pa.DayTimeIntervalScalar)
+ assert str(result[0]) == "125d0ms"
Review Comment:
```suggestion
assert str(result[0]) == "DayTime(days=125, milliseconds=0)"
```
##########
python/pyarrow/tests/test_scalars.py:
##########
@@ -529,6 +529,34 @@ def test_duration_nanos_nopandas():
arr[0].as_py()
+def test_day_time_and_month_interval():
+ arr1 = pa.array(
+ [datetime.datetime(2020, 1, 1), None, datetime.datetime(1900, 12, 13)],
+ type=pa.timestamp('s')
+ )
+ arr2 = pa.array(
+ [datetime.datetime(2020, 5, 5, 1, 30), None, None],
+ type=pa.timestamp('s')
+ )
+
+ arr = pc.month_interval_between(arr1, arr2)
+ assert isinstance(arr[0].as_py(), pa.Month)
+ assert arr[0].as_py() == pa.Month([4])
+ assert arr[0].as_py().months == 4
+ assert arr[0].value == arr[0].as_py()
+ assert arr[1].as_py(maps_as_pydicts="strict") is None
+ assert arr[1].value is None
+
+ arr = pc.day_time_interval_between(arr1, arr2)
+ assert isinstance(arr[0].as_py(), pa.DayTime)
+ assert arr[0].as_py() == pa.DayTime([125, 5400000])
+ assert arr[0].as_py().days == 125
+ assert arr[0].as_py().milliseconds == 5400000
+ assert arr[0].value == arr[0].as_py()
+ assert arr[1].as_py(maps_as_pydicts="strict") is None
+ assert arr[1].value is None
+
Review Comment:
```suggestion
assert repr(arr[1]) == "<pyarrow.DayTimeIntervalScalar: None>"
```
##########
python/pyarrow/types.pxi:
##########
@@ -4350,6 +4350,81 @@ def duration(unit):
return out
+def month_interval():
+ """
+ Create instance of an interval type representing months
+ between two dates.
+
+ Examples
+ --------
+ Create an instance of an month_interval type:
+
+ >>> import pyarrow as pa
+ >>> pa.month_interval()
+ DataType(month_interval)
+
+ The type can't be created from Python values yet, but
+ compute functions return it:
+
+ >>> import pyarrow as pa
+ >>> import pyarrow.compute as pc
+ >>> import datetime
+ >>> arr1 = pa.array([datetime.datetime(2020, 1, 1)],
type=pa.timestamp('s'))
+ >>> arr2 = pa.array([datetime.datetime(2020, 5, 5)],
type=pa.timestamp('s'))
+
+ Create an array with month_interval type:
+
+ >>> pc.month_interval_between(arr1, arr2)
+ <pyarrow.lib.MonthIntervalArray object at ...>
+ [
+ 4M
+ ]
+
+ or select a scalar with the same type:
+
+ >>> pc.month_interval_between(arr1, arr2)[0]
+ <pyarrow.MonthIntervalScalar: 4M>
+ """
+ return primitive_type(_Type_INTERVAL_MONTHS)
+
+
+def day_time_interval():
+ """
+ Create instance of an interval type representing day and
+ milliseconds between two dates.
+
+ Examples
+ --------
+ Create an instance of an day_time_interval type:
+
+ >>> import pyarrow as pa
+ >>> pa.day_time_interval()
+ DataType(day_time_interval)
+
+ The type can't be created from Python values yet, but
+ compute functions return it:
+
+ >>> import pyarrow.compute as pc
+ >>> import datetime
+ >>> arr1 = pa.array([datetime.datetime(2020, 1, 1)],
type=pa.timestamp('s'))
+ >>> arr2 = pa.array([datetime.datetime(2020, 5, 5)],
type=pa.timestamp('s'))
+
+ Create an array with day_time_interval type:
+
+ >>> pc.day_time_interval_between(arr1, arr2)
+ <pyarrow.lib.DayTimeIntervalArray object at ...>
+ [
+ 125d0ms
+ ]
+
+ or select a scalar with the same type:
+
+ >>> pc.day_time_interval_between(arr1, arr2)[0]
+ <pyarrow.DayTimeIntervalScalar: 125d0ms>
+ """
Review Comment:
```suggestion
<pyarrow.DayTimeIntervalScalar: DayTime(days=125, milliseconds=0)>
"""
```
--
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]