From 77dee58c7885aed05375a5f58fd5a8fdd4207bd5 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Wed, 24 Jun 2020 18:09:29 -0500 Subject: [PATCH 1/3] Recognize pandas.NaT when converting object arrays with from_pandas=True --- cpp/src/arrow/python/helpers.cc | 34 ++++++++++++++++++++++++- cpp/src/arrow/python/helpers.h | 4 +++ cpp/src/arrow/python/python_to_arrow.cc | 4 +++ python/pyarrow/tests/test_pandas.py | 10 ++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/python/helpers.cc b/cpp/src/arrow/python/helpers.cc index 4d50eb930061..05b0d9842e4b 100644 --- a/cpp/src/arrow/python/helpers.cc +++ b/cpp/src/arrow/python/helpers.cc @@ -21,6 +21,7 @@ #include "arrow/python/helpers.h" #include +#include #include #include #include @@ -254,6 +255,36 @@ bool PyFloat_IsNaN(PyObject* obj) { return PyFloat_Check(obj) && std::isnan(PyFloat_AsDouble(obj)); } +namespace { + +static std::once_flag pandas_static_initialized; +static OwnedRef pandas_NaTType; +static Status pandas_import_status; + +void GetPandasStaticSymbols() { + OwnedRef pandas; + Status s = ImportModule("pandas", &pandas); + if (!s.ok()) { + pandas_import_status = s; + return; + } + + OwnedRef nat_value; + s = ImportFromModule(pandas.obj(), "NaT", &nat_value); + if (!s.ok()) { + pandas_import_status = s; + return; + } + pandas_NaTType.reset(PyObject_Type(nat_value.obj())); +} + +} // namespace + +Status InitPandasStaticData() { + std::call_once(pandas_static_initialized, GetPandasStaticSymbols); + return pandas_import_status; +} + bool PandasObjectIsNull(PyObject* obj) { if (!MayHaveNaN(obj)) { return false; @@ -261,7 +292,8 @@ bool PandasObjectIsNull(PyObject* obj) { if (obj == Py_None) { return true; } - if (PyFloat_IsNaN(obj) || + PyTypeObject* NaT = reinterpret_cast(pandas_NaTType.obj()); + if (PyFloat_IsNaN(obj) || (NaT && PyObject_TypeCheck(obj, NaT)) || (internal::PyDecimal_Check(obj) && internal::PyDecimal_ISNAN(obj))) { return true; } diff --git a/cpp/src/arrow/python/helpers.h b/cpp/src/arrow/python/helpers.h index 92b4938c463d..a8500d652e73 100644 --- a/cpp/src/arrow/python/helpers.h +++ b/cpp/src/arrow/python/helpers.h @@ -68,6 +68,10 @@ Status ImportFromModule(PyObject* module, const std::string& name, OwnedRef* ref // \brief Check whether obj is an integer, independent of Python versions. inline bool IsPyInteger(PyObject* obj) { return PyLong_Check(obj); } +// \brief Import symbols from pandas that we need for various type-checking, +// like pandas.NaT or pandas.NA +Status InitPandasStaticData(); + // \brief Use pandas missing value semantics to check if a value is null ARROW_PYTHON_EXPORT bool PandasObjectIsNull(PyObject* obj); diff --git a/cpp/src/arrow/python/python_to_arrow.cc b/cpp/src/arrow/python/python_to_arrow.cc index 6e23b7f9eceb..3470c4054d6e 100644 --- a/cpp/src/arrow/python/python_to_arrow.cc +++ b/cpp/src/arrow/python/python_to_arrow.cc @@ -1171,6 +1171,10 @@ Status GetConverterFlat(const std::shared_ptr& type, bool strict_conve Status GetConverter(const std::shared_ptr& type, bool from_pandas, bool strict_conversions, std::unique_ptr* out) { + if (from_pandas) { + RETURN_NOT_OK(internal::InitPandasStaticData()); + } + switch (type->id()) { case Type::LIST: if (from_pandas) { diff --git a/python/pyarrow/tests/test_pandas.py b/python/pyarrow/tests/test_pandas.py index 791ce141823c..4c41b5fe2f33 100644 --- a/python/pyarrow/tests/test_pandas.py +++ b/python/pyarrow/tests/test_pandas.py @@ -1205,6 +1205,16 @@ def test_date_objects_typed(self): tm.assert_frame_equal(table_pandas_objects, expected_pandas_objects) + def test_NaT_in_object_array(self): + # ARROW-842 + values = np.array([datetime(2000, 1, 1), pd.NaT], dtype=object) + values_with_none = np.array([datetime(2000, 1, 1), None], + dtype=object) + result = pa.array(values, from_pandas=True) + expected = pa.array(values_with_none, from_pandas=True) + assert result.equals(expected) + assert result.null_count == 1 + def test_dates_from_integers(self): t1 = pa.date32() t2 = pa.date64() From 7dc638df234fdca6c723d8f65416beebbb8b364c Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Wed, 24 Jun 2020 19:30:30 -0500 Subject: [PATCH 2/3] Cleanups, do not error if pandas is unavailable --- cpp/src/arrow/python/helpers.cc | 18 +++++++++--------- cpp/src/arrow/python/helpers.h | 2 +- cpp/src/arrow/python/python_to_arrow.cc | 4 +++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cpp/src/arrow/python/helpers.cc b/cpp/src/arrow/python/helpers.cc index 05b0d9842e4b..23d661569500 100644 --- a/cpp/src/arrow/python/helpers.cc +++ b/cpp/src/arrow/python/helpers.cc @@ -258,31 +258,32 @@ bool PyFloat_IsNaN(PyObject* obj) { namespace { static std::once_flag pandas_static_initialized; -static OwnedRef pandas_NaTType; -static Status pandas_import_status; +static PyTypeObject* pandas_NaTType = nullptr; void GetPandasStaticSymbols() { OwnedRef pandas; Status s = ImportModule("pandas", &pandas); if (!s.ok()) { - pandas_import_status = s; return; } OwnedRef nat_value; s = ImportFromModule(pandas.obj(), "NaT", &nat_value); if (!s.ok()) { - pandas_import_status = s; return; } - pandas_NaTType.reset(PyObject_Type(nat_value.obj())); + PyObject* nat_type = PyObject_Type(nat_value.obj()); + pandas_NaTType = reinterpret_cast(nat_type); + + // PyObject_Type returns a new reference but we trust that pandas.NaT will + // outlive our use of this PyObject* + Py_DECREF(nat_type); } } // namespace -Status InitPandasStaticData() { +void InitPandasStaticData() { std::call_once(pandas_static_initialized, GetPandasStaticSymbols); - return pandas_import_status; } bool PandasObjectIsNull(PyObject* obj) { @@ -292,8 +293,7 @@ bool PandasObjectIsNull(PyObject* obj) { if (obj == Py_None) { return true; } - PyTypeObject* NaT = reinterpret_cast(pandas_NaTType.obj()); - if (PyFloat_IsNaN(obj) || (NaT && PyObject_TypeCheck(obj, NaT)) || + if (PyFloat_IsNaN(obj) || (pandas_NaTType && PyObject_TypeCheck(obj, pandas_NaTType)) || (internal::PyDecimal_Check(obj) && internal::PyDecimal_ISNAN(obj))) { return true; } diff --git a/cpp/src/arrow/python/helpers.h b/cpp/src/arrow/python/helpers.h index a8500d652e73..4d27c2c11a19 100644 --- a/cpp/src/arrow/python/helpers.h +++ b/cpp/src/arrow/python/helpers.h @@ -70,7 +70,7 @@ inline bool IsPyInteger(PyObject* obj) { return PyLong_Check(obj); } // \brief Import symbols from pandas that we need for various type-checking, // like pandas.NaT or pandas.NA -Status InitPandasStaticData(); +void InitPandasStaticData(); // \brief Use pandas missing value semantics to check if a value is null ARROW_PYTHON_EXPORT diff --git a/cpp/src/arrow/python/python_to_arrow.cc b/cpp/src/arrow/python/python_to_arrow.cc index 3470c4054d6e..bdaae4c2385e 100644 --- a/cpp/src/arrow/python/python_to_arrow.cc +++ b/cpp/src/arrow/python/python_to_arrow.cc @@ -1172,7 +1172,9 @@ Status GetConverterFlat(const std::shared_ptr& type, bool strict_conve Status GetConverter(const std::shared_ptr& type, bool from_pandas, bool strict_conversions, std::unique_ptr* out) { if (from_pandas) { - RETURN_NOT_OK(internal::InitPandasStaticData()); + // ARROW-842: If pandas is not installed then null checks will be less + // comprehensive, but that is okay. + internal::InitPandasStaticData(); } switch (type->id()) { From ac751ff5ae70c431b964a10150b82ab7ee86bdaa Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Thu, 25 Jun 2020 10:11:39 -0500 Subject: [PATCH 3/3] Also recognize pandas.NA --- cpp/src/arrow/python/helpers.cc | 15 +++++++++++---- python/pyarrow/tests/test_pandas.py | 9 +++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/cpp/src/arrow/python/helpers.cc b/cpp/src/arrow/python/helpers.cc index 23d661569500..852bf763afc6 100644 --- a/cpp/src/arrow/python/helpers.cc +++ b/cpp/src/arrow/python/helpers.cc @@ -259,6 +259,7 @@ namespace { static std::once_flag pandas_static_initialized; static PyTypeObject* pandas_NaTType = nullptr; +static PyObject* pandas_NA = nullptr; void GetPandasStaticSymbols() { OwnedRef pandas; @@ -267,17 +268,22 @@ void GetPandasStaticSymbols() { return; } - OwnedRef nat_value; - s = ImportFromModule(pandas.obj(), "NaT", &nat_value); + OwnedRef ref; + s = ImportFromModule(pandas.obj(), "NaT", &ref); if (!s.ok()) { return; } - PyObject* nat_type = PyObject_Type(nat_value.obj()); + PyObject* nat_type = PyObject_Type(ref.obj()); pandas_NaTType = reinterpret_cast(nat_type); // PyObject_Type returns a new reference but we trust that pandas.NaT will // outlive our use of this PyObject* Py_DECREF(nat_type); + + if (ImportFromModule(pandas.obj(), "NA", &ref).ok()) { + // If pandas.NA exists, retain a reference to it + pandas_NA = ref.obj(); + } } } // namespace @@ -293,7 +299,8 @@ bool PandasObjectIsNull(PyObject* obj) { if (obj == Py_None) { return true; } - if (PyFloat_IsNaN(obj) || (pandas_NaTType && PyObject_TypeCheck(obj, pandas_NaTType)) || + if (PyFloat_IsNaN(obj) || (pandas_NA && obj == pandas_NA) || + (pandas_NaTType && PyObject_TypeCheck(obj, pandas_NaTType)) || (internal::PyDecimal_Check(obj) && internal::PyDecimal_ISNAN(obj))) { return true; } diff --git a/python/pyarrow/tests/test_pandas.py b/python/pyarrow/tests/test_pandas.py index 4c41b5fe2f33..392a3221002b 100644 --- a/python/pyarrow/tests/test_pandas.py +++ b/python/pyarrow/tests/test_pandas.py @@ -1205,15 +1205,16 @@ def test_date_objects_typed(self): tm.assert_frame_equal(table_pandas_objects, expected_pandas_objects) - def test_NaT_in_object_array(self): + def test_object_null_values(self): # ARROW-842 - values = np.array([datetime(2000, 1, 1), pd.NaT], dtype=object) - values_with_none = np.array([datetime(2000, 1, 1), None], + NA = getattr(pd, 'NA', None) + values = np.array([datetime(2000, 1, 1), pd.NaT, NA], dtype=object) + values_with_none = np.array([datetime(2000, 1, 1), None, None], dtype=object) result = pa.array(values, from_pandas=True) expected = pa.array(values_with_none, from_pandas=True) assert result.equals(expected) - assert result.null_count == 1 + assert result.null_count == 2 def test_dates_from_integers(self): t1 = pa.date32()