Skip to content

BUG: Fix fields functions with readonly data, vaex#357 #27529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- Bug in :meth:`Series.__setitem__` incorrectly casting ``np.timedelta64("NaT")`` to ``np.datetime64("NaT")`` when inserting into a :class:`Series` with datetime64 dtype (:issue:`27311`)
-
- Bug in :meth:`Series.dt` property lookups when the underlying data is read-only (:issue:`27529`)
-


Expand Down
10 changes: 5 additions & 5 deletions pandas/_libs/tslibs/fields.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_time_micros(ndarray[int64_t] dtindex):

@cython.wraparound(False)
@cython.boundscheck(False)
def build_field_sarray(int64_t[:] dtindex):
def build_field_sarray(const int64_t[:] dtindex):
"""
Datetime as int64 representation to a structured array of fields
"""
Expand Down Expand Up @@ -87,7 +87,7 @@ def build_field_sarray(int64_t[:] dtindex):

@cython.wraparound(False)
@cython.boundscheck(False)
def get_date_name_field(int64_t[:] dtindex, object field, object locale=None):
def get_date_name_field(const int64_t[:] dtindex, object field, object locale=None):
"""
Given a int64-based datetime index, return array of strings of date
name based on requested field (e.g. weekday_name)
Expand Down Expand Up @@ -137,7 +137,7 @@ def get_date_name_field(int64_t[:] dtindex, object field, object locale=None):

@cython.wraparound(False)
@cython.boundscheck(False)
def get_start_end_field(int64_t[:] dtindex, object field,
def get_start_end_field(const int64_t[:] dtindex, object field,
object freqstr=None, int month_kw=12):
"""
Given an int64-based datetime index return array of indicators
Expand Down Expand Up @@ -380,7 +380,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,

@cython.wraparound(False)
@cython.boundscheck(False)
def get_date_field(int64_t[:] dtindex, object field):
def get_date_field(const int64_t[:] dtindex, object field):
"""
Given a int64-based datetime index, extract the year, month, etc.,
field and return an array of these values.
Expand Down Expand Up @@ -542,7 +542,7 @@ def get_date_field(int64_t[:] dtindex, object field):

@cython.wraparound(False)
@cython.boundscheck(False)
def get_timedelta_field(int64_t[:] tdindex, object field):
def get_timedelta_field(const int64_t[:] tdindex, object field):
"""
Given a int64-based timedelta index, extract the days, hrs, sec.,
field and return an array of these values.
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/tslibs/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np

from pandas._libs.tslibs import fields

import pandas.util.testing as tm


def test_fields_readonly():
# https://github.com/vaexio/vaex/issues/357
# fields functions should't raise when we pass read-only data
dtindex = np.arange(5, dtype=np.int64) * 10 ** 9 * 3600 * 24 * 32
dtindex.flags.writeable = False

result = fields.get_date_name_field(dtindex, "month_name")
expected = np.array(
["January", "February", "March", "April", "May"], dtype=np.object
)
tm.assert_numpy_array_equal(result, expected)

result = fields.get_date_field(dtindex, "Y")
expected = np.array([1970, 1970, 1970, 1970, 1970], dtype=np.int32)
tm.assert_numpy_array_equal(result, expected)

result = fields.get_start_end_field(dtindex, "is_month_start", None)
expected = np.array([True, False, False, False, False], dtype=np.bool_)
tm.assert_numpy_array_equal(result, expected)

# treat dtindex as timedeltas for this next one
result = fields.get_timedelta_field(dtindex, "days")
expected = np.arange(5, dtype=np.int32) * 32
tm.assert_numpy_array_equal(result, expected)