From 11aad905e54f2e2caf3c9d17675d809c8c6ee3a9 Mon Sep 17 00:00:00 2001 From: fjetter Date: Thu, 17 May 2018 16:16:14 +0200 Subject: [PATCH 1/4] python: Allow date and datetime subclassing --- cpp/src/arrow/python/numpy_to_arrow.cc | 8 ++-- cpp/src/arrow/python/python_to_arrow.cc | 2 +- python/pyarrow/tests/test_convert_builtin.py | 49 ++++++++++++++++++++ python/pyarrow/tests/test_convert_pandas.py | 32 +++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/python/numpy_to_arrow.cc b/cpp/src/arrow/python/numpy_to_arrow.cc index 2d4d91cde00d..a1a48e22bfee 100644 --- a/cpp/src/arrow/python/numpy_to_arrow.cc +++ b/cpp/src/arrow/python/numpy_to_arrow.cc @@ -724,7 +724,7 @@ Status NumPyConverter::ConvertDates() { obj = objects[i]; if ((have_mask && mask_values[i]) || internal::PandasObjectIsNull(obj)) { RETURN_NOT_OK(builder.AppendNull()); - } else if (PyDate_CheckExact(obj)) { + } else if (PyDate_Check(obj)) { RETURN_NOT_OK(builder.Append(UnboxDate::Unbox(obj))); } else { std::stringstream ss; @@ -1071,11 +1071,11 @@ Status NumPyConverter::ConvertObjectsInfer() { return ConvertBooleans(); } else if (PyObject_is_integer(obj)) { return ConvertObjectIntegers(); - } else if (PyDate_CheckExact(obj)) { + } else if (PyDateTime_Check(obj)) { + return ConvertDateTimes(); + } else if (PyDate_Check(obj)) { // We could choose Date32 or Date64 return ConvertDates(); - } else if (PyDateTime_CheckExact(obj)) { - return ConvertDateTimes(); } else if (PyTime_Check(obj)) { return ConvertTimes(); } else if (PyObject_IsInstance(obj, decimal_type_.obj()) == 1) { diff --git a/cpp/src/arrow/python/python_to_arrow.cc b/cpp/src/arrow/python/python_to_arrow.cc index 23445ee26d0e..f48d728046df 100644 --- a/cpp/src/arrow/python/python_to_arrow.cc +++ b/cpp/src/arrow/python/python_to_arrow.cc @@ -522,7 +522,7 @@ Status Append(PyObject* context, PyObject* elem, SequenceBuilder* builder, subdicts, blobs_out)); } else if (elem == Py_None) { RETURN_NOT_OK(builder->AppendNone()); - } else if (PyDateTime_CheckExact(elem)) { + } else if (PyDateTime_Check(elem)) { PyDateTime_DateTime* datetime = reinterpret_cast(elem); RETURN_NOT_OK(builder->AppendDate64(PyDateTime_to_us(datetime))); } else if (is_buffer(elem)) { diff --git a/python/pyarrow/tests/test_convert_builtin.py b/python/pyarrow/tests/test_convert_builtin.py index 8a8e5542ee2b..63c6ce4cb39a 100644 --- a/python/pyarrow/tests/test_convert_builtin.py +++ b/python/pyarrow/tests/test_convert_builtin.py @@ -477,6 +477,55 @@ def test_sequence_timestamp_with_unit(): 23, 34, 123456) +def test_datetime_subclassing(): + class MyDate(datetime.date): + pass + data = [ + MyDate(2007, 7, 13), + ] + date_type = pa.date32() + arr_date = pa.array(data, type=date_type) + assert len(arr_date) == 1 + assert arr_date.type == date_type + assert arr_date[0].as_py() == datetime.date(2007, 7, 13) + + class MyDatetime(datetime): + pass + + data = [ + MyDatetime(2007, 7, 13, 1, 23, 34, 123456), + ] + + s = pa.timestamp('s') + ms = pa.timestamp('ms') + us = pa.timestamp('us') + ns = pa.timestamp('ns') + + arr_s = pa.array(data, type=s) + assert len(arr_s) == 1 + assert arr_s.type == s + assert arr_s[0].as_py() == datetime.datetime(2007, 7, 13, 1, + 23, 34, 0) + + arr_ms = pa.array(data, type=ms) + assert len(arr_ms) == 1 + assert arr_ms.type == ms + assert arr_ms[0].as_py() == datetime.datetime(2007, 7, 13, 1, + 23, 34, 123000) + + arr_us = pa.array(data, type=us) + assert len(arr_us) == 1 + assert arr_us.type == us + assert arr_us[0].as_py() == datetime.datetime(2007, 7, 13, 1, + 23, 34, 123456) + + arr_ns = pa.array(data, type=ns) + assert len(arr_ns) == 1 + assert arr_ns.type == ns + assert arr_ns[0].as_py() == datetime.datetime(2007, 7, 13, 1, + 23, 34, 123456) + + def test_sequence_timestamp_from_int_with_unit(): data = [1] diff --git a/python/pyarrow/tests/test_convert_pandas.py b/python/pyarrow/tests/test_convert_pandas.py index 6bdbcca01c6a..c23c69b1f100 100644 --- a/python/pyarrow/tests/test_convert_pandas.py +++ b/python/pyarrow/tests/test_convert_pandas.py @@ -814,6 +814,38 @@ def test_python_datetime(self): }) tm.assert_frame_equal(expected_df, result) + def test_python_datetime_subclass(self): + + class MyDatetime(datetime): + pass + + date_array = [MyDatetime(2000, 1, 1)] + df = pd.DataFrame({"datetime": pd.Series(date_array, dtype=object)}) + + table = pa.Table.from_pandas(df) + assert isinstance(table[0].data.chunk(0), pa.TimestampArray) + + result = table.to_pandas() + expected_df = pd.DataFrame({"datetime": date_array}) + tm.assert_frame_equal(expected_df, result) + + def test_python_date_subclass(self): + + class MyDate(date): + pass + + date_array = [MyDate(2000, 1, 1)] + df = pd.DataFrame({"date": pd.Series(date_array, dtype=object)}) + + table = pa.Table.from_pandas(df) + assert isinstance(table[0].data.chunk(0), pa.Date32Array) + + result = table.to_pandas() + expected_df = pd.DataFrame( + {"date": np.array(["2000-01-01"], dtype="datetime64[ns]")} + ) + tm.assert_frame_equal(expected_df, result) + def test_datetime64_to_date32(self): # ARROW-1718 arr = pa.array([date(2017, 10, 23), None]) From 927bae3d007ec15eb858f0c80f076cae3df28f70 Mon Sep 17 00:00:00 2001 From: fjetter Date: Sun, 20 May 2018 14:38:58 +0200 Subject: [PATCH 2/4] fix tests --- python/pyarrow/tests/test_convert_builtin.py | 2 +- python/pyarrow/tests/test_convert_pandas.py | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/python/pyarrow/tests/test_convert_builtin.py b/python/pyarrow/tests/test_convert_builtin.py index 63c6ce4cb39a..a4b2151bdf40 100644 --- a/python/pyarrow/tests/test_convert_builtin.py +++ b/python/pyarrow/tests/test_convert_builtin.py @@ -489,7 +489,7 @@ class MyDate(datetime.date): assert arr_date.type == date_type assert arr_date[0].as_py() == datetime.date(2007, 7, 13) - class MyDatetime(datetime): + class MyDatetime(datetime.datetime): pass data = [ diff --git a/python/pyarrow/tests/test_convert_pandas.py b/python/pyarrow/tests/test_convert_pandas.py index c23c69b1f100..fb1e2b9039fd 100644 --- a/python/pyarrow/tests/test_convert_pandas.py +++ b/python/pyarrow/tests/test_convert_pandas.py @@ -15,24 +15,21 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. - -from collections import OrderedDict - -from datetime import date, datetime, time, timedelta import decimal import json - -import pytest +from collections import OrderedDict +from datetime import date, datetime, time, timedelta +from distutils.version import LooseVersion import numpy as np import numpy.testing as npt - import pandas as pd import pandas.util.testing as tm +import pytest -from pyarrow.compat import PY2 import pyarrow as pa import pyarrow.types as patypes +from pyarrow.compat import PY2 from .pandas_examples import dataframe_with_arrays, dataframe_with_lists @@ -814,12 +811,16 @@ def test_python_datetime(self): }) tm.assert_frame_equal(expected_df, result) + @pytest.mark.xfail( + LooseVersion(pd.__version__) >= "0.23.0", + reason="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/pandas-dev/pandas/issues/21142", + ) def test_python_datetime_subclass(self): class MyDatetime(datetime): pass - date_array = [MyDatetime(2000, 1, 1)] + date_array = [MyDatetime(2000, 1, 1, 1, 1, 1)] df = pd.DataFrame({"datetime": pd.Series(date_array, dtype=object)}) table = pa.Table.from_pandas(df) @@ -827,6 +828,7 @@ class MyDatetime(datetime): result = table.to_pandas() expected_df = pd.DataFrame({"datetime": date_array}) + tm.assert_frame_equal(expected_df, result) def test_python_date_subclass(self): From 79e420adfa727c0d776955e4085fda3a91a65287 Mon Sep 17 00:00:00 2001 From: fjetter Date: Fri, 25 May 2018 08:12:12 +0200 Subject: [PATCH 3/4] convert_pandas: cast datetime subclass explicitly during tests --- python/pyarrow/tests/test_convert_pandas.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/python/pyarrow/tests/test_convert_pandas.py b/python/pyarrow/tests/test_convert_pandas.py index fb1e2b9039fd..7e9f20a16e96 100644 --- a/python/pyarrow/tests/test_convert_pandas.py +++ b/python/pyarrow/tests/test_convert_pandas.py @@ -19,7 +19,6 @@ import json from collections import OrderedDict from datetime import date, datetime, time, timedelta -from distutils.version import LooseVersion import numpy as np import numpy.testing as npt @@ -811,14 +810,12 @@ def test_python_datetime(self): }) tm.assert_frame_equal(expected_df, result) - @pytest.mark.xfail( - LooseVersion(pd.__version__) >= "0.23.0", - reason="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/pandas-dev/pandas/issues/21142", - ) def test_python_datetime_subclass(self): class MyDatetime(datetime): pass + # see https://github.com/pandas-dev/pandas/issues/21142 + nanosecond = 0.0 date_array = [MyDatetime(2000, 1, 1, 1, 1, 1)] df = pd.DataFrame({"datetime": pd.Series(date_array, dtype=object)}) @@ -828,7 +825,8 @@ class MyDatetime(datetime): result = table.to_pandas() expected_df = pd.DataFrame({"datetime": date_array}) - + # https://github.com/pandas-dev/pandas/issues/21142 + expected_df["datetime"] = pd.to_datetime(expected_df["datetime"]) tm.assert_frame_equal(expected_df, result) def test_python_date_subclass(self): From cd76b3e0d9d4d68e91f69ac1a94965805df8541f Mon Sep 17 00:00:00 2001 From: fjetter Date: Thu, 17 May 2018 16:16:14 +0200 Subject: [PATCH 4/4] python: Allow date and datetime subclassing --- python/pyarrow/tests/test_convert_pandas.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/pyarrow/tests/test_convert_pandas.py b/python/pyarrow/tests/test_convert_pandas.py index 7e9f20a16e96..bbe74ac16ee9 100644 --- a/python/pyarrow/tests/test_convert_pandas.py +++ b/python/pyarrow/tests/test_convert_pandas.py @@ -813,7 +813,6 @@ def test_python_datetime(self): def test_python_datetime_subclass(self): class MyDatetime(datetime): - pass # see https://github.com/pandas-dev/pandas/issues/21142 nanosecond = 0.0 @@ -825,8 +824,10 @@ class MyDatetime(datetime): result = table.to_pandas() expected_df = pd.DataFrame({"datetime": date_array}) + # https://github.com/pandas-dev/pandas/issues/21142 expected_df["datetime"] = pd.to_datetime(expected_df["datetime"]) + tm.assert_frame_equal(expected_df, result) def test_python_date_subclass(self):