update for feedback

This commit is contained in:
Joris Van den Bossche authored and Krisztián Szűcs committed 2019-10-05 12:03:33 +02:00
1 parent f4d6b4b645
commit 56bd1b3b38
7 files changed
+54 -56

No files matched your search

+17 -17
View File
@@ -232,24 +232,24 @@ inline void set_numpy_metadata(int type, const DataType* datatype, PyArray_Descr
date_dtype->meta.base = NPY_FR_D;
}
} else if (type == NPY_TIMEDELTA) {
auto date_dtype = reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(out->c_metadata);
if (datatype->id() == Type::DURATION) {
const auto& duration_type = checked_cast<const DurationType&>(*datatype);
DCHECK_EQ(datatype->id(), Type::DURATION);
auto timedelta_dtype =
reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(out->c_metadata);
const auto& duration_type = checked_cast<const DurationType&>(*datatype);
switch (duration_type.unit()) {
case DurationType::Unit::SECOND:
date_dtype->meta.base = NPY_FR_s;
break;
case DurationType::Unit::MILLI:
date_dtype->meta.base = NPY_FR_ms;
break;
case DurationType::Unit::MICRO:
date_dtype->meta.base = NPY_FR_us;
break;
case DurationType::Unit::NANO:
date_dtype->meta.base = NPY_FR_ns;
break;
}
switch (duration_type.unit()) {
case DurationType::Unit::SECOND:
timedelta_dtype->meta.base = NPY_FR_s;
break;
case DurationType::Unit::MILLI:
timedelta_dtype->meta.base = NPY_FR_ms;
break;
case DurationType::Unit::MICRO:
timedelta_dtype->meta.base = NPY_FR_us;
break;
case DurationType::Unit::NANO:
timedelta_dtype->meta.base = NPY_FR_ns;
break;
}
}
}
+17 -13
View File
@@ -116,22 +116,24 @@ inline int64_t PyDateTime_to_ns(PyDateTime_DateTime* pydatetime) {
ARROW_PYTHON_EXPORT
inline int64_t PyDelta_to_s(PyDateTime_Delta* pytimedelta) {
int64_t total_seconds = 0;
// TODO(py2) change to "PyDateTime_DELTA_GET_SECONDS/DAYS(pytimedelta)"
total_seconds +=
PyLong_AsLongLong(PyObject_GetAttrString((PyObject*)pytimedelta, "seconds"));
total_seconds +=
PyLong_AsLongLong(PyObject_GetAttrString((PyObject*)pytimedelta, "days")) * 24 *
3600;
#if PY_VERSION_HEX >= 0x03000000
total_seconds += PyDateTime_DELTA_GET_SECONDS(pytimedelta);
total_seconds += PyDateTime_DELTA_GET_DAYS(pytimedelta) * 24 * 3600;
#else
total_seconds += pytimedelta->seconds;
total_seconds += pytimedelta->days * 24 * 3600;
#endif
return total_seconds;
}
ARROW_PYTHON_EXPORT
inline int64_t PyDelta_to_ms(PyDateTime_Delta* pytimedelta) {
int64_t total_ms = PyDelta_to_s(pytimedelta) * 1000;
// TODO(py2) change to "PyDateTime_DELTA_GET_MICROSECONDS(pytimedelta) / 1000"
total_ms +=
PyLong_AsLongLong(PyObject_GetAttrString((PyObject*)pytimedelta, "microseconds")) /
1000;
#if PY_VERSION_HEX >= 0x03000000
total_ms += PyDateTime_DELTA_GET_MICROSECONDS(pytimedelta) / 1000;
#else
total_ms += pytimedelta->microseconds / 1000;
#endif
return total_ms;
}
@@ -139,9 +141,11 @@ ARROW_PYTHON_EXPORT
inline int64_t PyDelta_to_us(PyDateTime_Delta* pytimedelta) {
int64_t total_us = 0;
total_us += PyDelta_to_s(pytimedelta) * 1000 * 1000;
// TODO(py2) change to "PyDateTime_DELTA_GET_MICROSECONDS(pytimedelta)"
total_us +=
PyLong_AsLongLong(PyObject_GetAttrString((PyObject*)pytimedelta, "microseconds"));
#if PY_VERSION_HEX >= 0x03000000
total_us += PyDateTime_DELTA_GET_MICROSECONDS(pytimedelta);
#else
total_us += pytimedelta->microseconds;
#endif
return total_us;
}
+1 -4
View File
@@ -139,10 +139,7 @@ struct npy_traits<NPY_TIMEDELTA> {
static constexpr bool supports_nulls = true;
static inline bool isnull(int64_t v) {
// NaT = -2**63
// = -0x8000000000000000
// = -9223372036854775808;
// = std::numeric_limits<int64_t>::min()
// NaT = -2**63 = std::numeric_limits<int64_t>::min()
return v == std::numeric_limits<int64_t>::min();
}
};
+1 -4
View File
@@ -406,7 +406,6 @@ cdef class TimestampValue(ArrayValue):
cdef dict _TIMEDELTA_CONVERSION_FUNCTIONS = {}
cdef c_bool _timedelta_conversion_initialized = False
def _nanoseconds_to_timedelta_safe(v):
@@ -422,8 +421,7 @@ def _nanoseconds_to_timedelta_safe(v):
def _timedelta_conversion_functions():
global _timedelta_conversion_initialized
if _timedelta_conversion_initialized:
if _TIMEDELTA_CONVERSION_FUNCTIONS:
return _TIMEDELTA_CONVERSION_FUNCTIONS
_TIMEDELTA_CONVERSION_FUNCTIONS.update({
@@ -441,7 +439,6 @@ def _timedelta_conversion_functions():
except ImportError:
pass
_timedelta_conversion_initialized = True
return _TIMEDELTA_CONVERSION_FUNCTIONS
+2
View File
@@ -1265,10 +1265,12 @@ def test_array_from_numpy_timedelta(dtype, type):
assert arr.type == type
expected = pa.array(data, type=type)
assert arr.equals(expected)
assert arr.to_pylist() == data
# from list of numpy scalars
arr = pa.array(list(np.array(data, dtype=dtype)))
assert arr.equals(expected)
assert arr.to_pylist() == data
def test_array_from_numpy_timedelta_incorrect_unit():
+15 -17
View File
@@ -868,7 +868,7 @@ def test_datetime_subclassing():
23, 34, 123456)
data = [
MyTimedelta(1, 1, 1000),
MyTimedelta(123, 456, 1002),
]
s = pa.duration('s')
@@ -878,22 +878,22 @@ def test_datetime_subclassing():
arr_s = pa.array(data)
assert len(arr_s) == 1
assert arr_s.type == us
assert arr_s[0].as_py() == datetime.timedelta(1, 1, 1000)
assert arr_s[0].as_py() == datetime.timedelta(123, 456, 1002)
arr_s = pa.array(data, type=s)
assert len(arr_s) == 1
assert arr_s.type == s
assert arr_s[0].as_py() == datetime.timedelta(1, 1)
assert arr_s[0].as_py() == datetime.timedelta(123, 456)
arr_ms = pa.array(data, type=ms)
assert len(arr_ms) == 1
assert arr_ms.type == ms
assert arr_ms[0].as_py() == datetime.timedelta(1, 1, 1000)
assert arr_ms[0].as_py() == datetime.timedelta(123, 456, 1000)
arr_us = pa.array(data, type=us)
assert len(arr_us) == 1
assert arr_us.type == us
assert arr_us[0].as_py() == datetime.timedelta(1, 1, 1000)
assert arr_us[0].as_py() == datetime.timedelta(123, 456, 1002)
@pytest.mark.xfail(not _pandas_api.have_pandas,
@@ -966,8 +966,8 @@ def test_sequence_timestamp_from_int_with_unit():
@pytest.mark.parametrize('np_scalar', [True, False])
def test_sequence_duration(np_scalar):
td1 = datetime.timedelta(1, 1, 1)
td2 = datetime.timedelta(1, 100)
td1 = datetime.timedelta(2, 3601, 1)
td2 = datetime.timedelta(1, 100, 1000)
if np_scalar:
data = [np.timedelta64(td1), None, np.timedelta64(td2)]
else:
@@ -985,12 +985,12 @@ def test_sequence_duration(np_scalar):
@pytest.mark.parametrize('unit', ['s', 'ms', 'us', 'ns'])
def test_sequence_duration_with_unit(unit):
data = [
datetime.timedelta(1, 1, 1001),
datetime.timedelta(3, 22, 1001),
]
expected = {'s': datetime.timedelta(1, 1),
'ms': datetime.timedelta(1, 1, 1000),
'us': datetime.timedelta(1, 1, 1001),
'ns': datetime.timedelta(1, 1, 1001)}
expected = {'s': datetime.timedelta(3, 22),
'ms': datetime.timedelta(3, 22, 1000),
'us': datetime.timedelta(3, 22, 1001),
'ns': datetime.timedelta(3, 22, 1001)}
ty = pa.duration(unit)
@@ -1002,15 +1002,13 @@ def test_sequence_duration_with_unit(unit):
@pytest.mark.parametrize('unit', ['s', 'ms', 'us', 'ns'])
def test_sequence_duration_from_int_with_unit(unit):
data = [1]
ty = pa.timestamp(unit)
data = [5]
ty = pa.duration(unit)
arr = pa.array(data, type=ty)
assert len(arr) == 1
assert arr.type == ty
assert arr[0].value == 1
assert arr[0].as_py()
assert arr[0].value == 5
def test_sequence_duration_nested_lists():
+1 -1
View File
@@ -324,7 +324,7 @@ class TestScalars(unittest.TestCase):
# Non-zero nanos work fine
arr = pa.array([946684800000000001], type=pa.duration('ns'))
arr[0].as_py() == pd.Timedelta(946684800000000001, unit='ns')
assert arr[0].as_py() == pd.Timedelta(946684800000000001, unit='ns')
@pytest.mark.nopandas
def test_duration_nanos_nopandas(self):