Skip to content

Commit 9ec7212

Browse files
jbrockmendeljreback
authored andcommitted
CLN: cleanup libs cimports, remove is_timestamp (pandas-dev#18663)
1 parent a7f6714 commit 9ec7212

File tree

11 files changed

+78
-117
lines changed

11 files changed

+78
-117
lines changed

pandas/_libs/parsers.pyx

+44-45
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
# Copyright (c) 2012, Lambda Foundry, Inc.
22
# See LICENSE for the license
3+
import os
4+
import sys
5+
import time
6+
import warnings
7+
8+
from csv import QUOTE_MINIMAL, QUOTE_NONNUMERIC, QUOTE_NONE
39

410
from libc.stdio cimport fopen, fclose
511
from libc.stdlib cimport malloc, free
612
from libc.string cimport strncpy, strlen, strcmp, strcasecmp
7-
cimport libc.stdio as stdio
8-
import warnings
913

10-
from csv import QUOTE_MINIMAL, QUOTE_NONNUMERIC, QUOTE_NONE
14+
cimport cython
15+
from cython cimport Py_ssize_t
16+
1117
from cpython cimport (PyObject, PyBytes_FromString,
1218
PyBytes_AsString, PyBytes_Check,
1319
PyUnicode_Check, PyUnicode_AsUTF8String,
1420
PyErr_Occurred, PyErr_Fetch)
1521
from cpython.ref cimport Py_XDECREF
16-
from pandas.errors import (ParserError, DtypeWarning,
17-
EmptyDataError, ParserWarning)
1822

19-
# Import CParserError as alias of ParserError for backwards compatibility.
20-
# Ultimately, we want to remove this import. See gh-12665 and gh-14479.
21-
CParserError = ParserError
2223

2324
cdef extern from "Python.h":
2425
object PyUnicode_FromString(char *v)
@@ -29,15 +30,24 @@ cdef extern from "Python.h":
2930
cdef extern from "stdlib.h":
3031
void memcpy(void *dst, void *src, size_t n)
3132

32-
cimport cython
33-
cimport numpy as cnp
3433

34+
import numpy as np
35+
cimport numpy as cnp
3536
from numpy cimport ndarray, uint8_t, uint64_t, int64_t
37+
cnp.import_array()
3638

37-
import numpy as np
38-
cimport util
39+
from util cimport UINT64_MAX, INT64_MAX, INT64_MIN
40+
import lib
41+
42+
from khash cimport (
43+
khiter_t,
44+
kh_str_t, kh_init_str, kh_put_str, kh_exist_str,
45+
kh_get_str, kh_destroy_str,
46+
kh_float64_t, kh_get_float64, kh_destroy_float64,
47+
kh_put_float64, kh_init_float64,
48+
kh_strbox_t, kh_put_strbox, kh_get_strbox, kh_init_strbox,
49+
kh_destroy_strbox)
3950

40-
import pandas._libs.lib as lib
4151
import pandas.compat as compat
4252
from pandas.core.dtypes.common import (
4353
is_categorical_dtype, CategoricalDtype,
@@ -47,55 +57,44 @@ from pandas.core.dtypes.common import (
4757
pandas_dtype)
4858
from pandas.core.categorical import Categorical
4959
from pandas.core.dtypes.concat import union_categoricals
50-
5160
import pandas.io.common as com
5261

53-
import time
54-
import os
55-
56-
cnp.import_array()
62+
from pandas.errors import (ParserError, DtypeWarning,
63+
EmptyDataError, ParserWarning)
5764

58-
from khash cimport (
59-
khiter_t,
60-
kh_str_t, kh_init_str, kh_put_str, kh_exist_str,
61-
kh_get_str, kh_destroy_str,
62-
kh_float64_t, kh_get_float64, kh_destroy_float64,
63-
kh_put_float64, kh_init_float64,
64-
kh_strbox_t, kh_put_strbox, kh_get_strbox, kh_init_strbox,
65-
kh_destroy_strbox)
65+
# Import CParserError as alias of ParserError for backwards compatibility.
66+
# Ultimately, we want to remove this import. See gh-12665 and gh-14479.
67+
CParserError = ParserError
6668

67-
import sys
6869

6970
cdef bint PY3 = (sys.version_info[0] >= 3)
7071

7172
cdef double INF = <double> np.inf
7273
cdef double NEGINF = -INF
7374

74-
cdef extern from "headers/stdint.h":
75-
enum: UINT8_MAX
76-
enum: UINT16_MAX
77-
enum: UINT32_MAX
78-
enum: UINT64_MAX
79-
enum: INT8_MIN
80-
enum: INT8_MAX
81-
enum: INT16_MIN
82-
enum: INT16_MAX
83-
enum: INT32_MAX
84-
enum: INT32_MIN
85-
enum: INT64_MAX
86-
enum: INT64_MIN
87-
88-
cdef extern from "headers/portable.h":
89-
pass
9075

9176
cdef extern from "errno.h":
9277
int errno
9378

79+
cdef extern from "headers/portable.h":
80+
# I *think* this is here so that strcasecmp is defined on Windows
81+
# so we don't get
82+
# `parsers.obj : error LNK2001: unresolved external symbol strcasecmp`
83+
# in Appveyor.
84+
# In a sane world, the `from libc.string cimport` above would fail
85+
# loudly.
86+
pass
87+
9488
try:
9589
basestring
9690
except NameError:
9791
basestring = str
9892

93+
cdef extern from "src/numpy_helper.h":
94+
object sarr_from_data(cnp.dtype, int length, void* data)
95+
void transfer_object_column(char *dst, char *src, size_t stride,
96+
size_t length)
97+
9998
cdef extern from "parser/tokenizer.h":
10099

101100
ctypedef enum ParserState:
@@ -2360,7 +2359,7 @@ def _to_structured_array(dict columns, object names, object usecols):
23602359
# We own the data.
23612360
buf = <char*> malloc(length * stride)
23622361

2363-
recs = util.sarr_from_data(dt, length, buf)
2362+
recs = sarr_from_data(dt, length, buf)
23642363
assert(recs.flags.owndata)
23652364

23662365
for i in range(nfields):
@@ -2385,7 +2384,7 @@ cdef _fill_structured_column(char *dst, char* src, int64_t elsize,
23852384
int64_t i
23862385

23872386
if incref:
2388-
util.transfer_object_column(dst, src, stride, length)
2387+
transfer_object_column(dst, src, stride, length)
23892388
else:
23902389
for i in range(length):
23912390
memcpy(dst, src, elsize)

pandas/_libs/src/util.pxd

-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ cdef extern from "numpy_helper.h":
1818
object get_value_1d(ndarray, Py_ssize_t)
1919
char *get_c_string(object) except NULL
2020
object char_to_string(char*)
21-
void transfer_object_column(char *dst, char *src, size_t stride,
22-
size_t length)
23-
object sarr_from_data(cnp.dtype, int length, void* data)
2421
object unbox_if_zerodim(object arr)
2522

2623
ctypedef fused numeric:
@@ -100,8 +97,6 @@ cdef inline set_value_at(ndarray arr, object loc, object value):
10097

10198
set_value_at_unsafe(arr, loc, value)
10299

103-
cdef inline int is_contiguous(ndarray arr):
104-
return cnp.PyArray_CHKFLAGS(arr, cnp.NPY_C_CONTIGUOUS)
105100

106101
cdef inline is_array(object o):
107102
return cnp.PyArray_Check(o)

pandas/_libs/tslib.pyx

+18-35
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
3-
# cython: linetrace=False
4-
# distutils: define_macros=CYTHON_TRACE=0
5-
# distutils: define_macros=CYTHON_TRACE_NOGIL=0
63

74
cimport numpy as np
85
from numpy cimport int64_t, ndarray, float64_t
96
import numpy as np
107
np.import_array()
118

129

13-
from cpython cimport PyTypeObject, PyFloat_Check
14-
15-
cdef extern from "Python.h":
16-
cdef PyTypeObject *Py_TYPE(object)
10+
from cpython cimport PyFloat_Check
1711

1812
from util cimport (is_integer_object, is_float_object, is_string_object,
1913
is_datetime64_object)
2014

2115
from cpython.datetime cimport (PyDateTime_Check, PyDate_Check,
16+
PyDateTime_CheckExact,
2217
PyDateTime_IMPORT,
2318
timedelta, datetime, date)
2419
# import datetime C API
@@ -47,10 +42,8 @@ UTC = pytz.utc
4742

4843
from tslibs.timedeltas cimport cast_from_unit
4944
from tslibs.timedeltas import Timedelta
50-
from tslibs.timezones cimport (
51-
is_utc, is_tzlocal, is_fixed_offset,
52-
treat_tz_as_pytz,
53-
get_dst_info)
45+
from tslibs.timezones cimport (is_utc, is_tzlocal, is_fixed_offset,
46+
treat_tz_as_pytz, get_dst_info)
5447
from tslibs.conversion cimport (tz_convert_single, _TSObject,
5548
convert_datetime_to_tsobject,
5649
get_datetime64_nanos)
@@ -204,13 +197,6 @@ def ints_to_pytimedelta(ndarray[int64_t] arr, box=False):
204197
return result
205198

206199

207-
cdef PyTypeObject* ts_type = <PyTypeObject*> Timestamp
208-
209-
210-
cdef inline bint is_timestamp(object o):
211-
return Py_TYPE(o) == ts_type # isinstance(o, Timestamp)
212-
213-
214200
def _test_parse_iso8601(object ts):
215201
"""
216202
TESTING ONLY: Parse string into Timestamp using iso8601 parser. Used
@@ -333,14 +319,6 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
333319
return result
334320

335321

336-
# const for parsers
337-
338-
_MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
339-
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
340-
_MONTH_NUMBERS = {k: i for i, k in enumerate(_MONTHS)}
341-
_MONTH_ALIASES = {(k + 1): v for k, v in enumerate(_MONTHS)}
342-
343-
344322
cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
345323
"""
346324
convert the ndarray according to the unit
@@ -360,7 +338,7 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
360338
bint is_ignore = errors=='ignore'
361339
bint is_coerce = errors=='coerce'
362340
bint is_raise = errors=='raise'
363-
bint need_to_iterate=True
341+
bint need_to_iterate = True
364342
ndarray[int64_t] iresult
365343
ndarray[object] oresult
366344

@@ -383,7 +361,7 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
383361
mask = iresult == iNaT
384362
iresult[mask] = 0
385363
fvalues = iresult.astype('f8') * m
386-
need_to_iterate=False
364+
need_to_iterate = False
387365
except:
388366
pass
389367

@@ -394,7 +372,7 @@ cpdef array_with_unit_to_datetime(ndarray values, unit, errors='coerce'):
394372
or (fvalues > _NS_UPPER_BOUND).any()):
395373
raise OutOfBoundsDatetime(
396374
"cannot convert input with unit '{0}'".format(unit))
397-
result = (iresult *m).astype('M8[ns]')
375+
result = (iresult * m).astype('M8[ns]')
398376
iresult = result.view('i8')
399377
iresult[mask] = iNaT
400378
return result
@@ -545,7 +523,8 @@ cpdef array_to_datetime(ndarray[object] values, errors='raise',
545523
'utc=True')
546524
else:
547525
iresult[i] = pydatetime_to_dt64(val, &dts)
548-
if is_timestamp(val):
526+
if not PyDateTime_CheckExact(val):
527+
# i.e. a Timestamp object
549528
iresult[i] += val.nanosecond
550529
try:
551530
check_dts_bounds(&dts)
@@ -752,11 +731,15 @@ cpdef normalize_date(object dt):
752731
-------
753732
normalized : datetime.datetime or Timestamp
754733
"""
755-
if is_timestamp(dt):
756-
return dt.replace(hour=0, minute=0, second=0, microsecond=0,
757-
nanosecond=0)
758-
elif PyDateTime_Check(dt):
759-
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
734+
if PyDateTime_Check(dt):
735+
if not PyDateTime_CheckExact(dt):
736+
# i.e. a Timestamp object
737+
return dt.replace(hour=0, minute=0, second=0, microsecond=0,
738+
nanosecond=0)
739+
else:
740+
# regular datetime object
741+
return dt.replace(hour=0, minute=0, second=0, microsecond=0)
742+
# TODO: Make sure DST crossing is handled correctly here
760743
elif PyDate_Check(dt):
761744
return datetime(dt.year, dt.month, dt.day)
762745
else:

pandas/_libs/tslibs/conversion.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ from util cimport (is_string_object,
3232
is_integer_object, is_float_object)
3333

3434
from timedeltas cimport cast_from_unit
35-
from timezones cimport (
36-
is_utc, is_tzlocal, is_fixed_offset,
37-
treat_tz_as_dateutil, treat_tz_as_pytz,
38-
get_utcoffset, get_dst_info, get_timezone, maybe_get_tz)
35+
from timezones cimport (is_utc, is_tzlocal, is_fixed_offset,
36+
treat_tz_as_dateutil, treat_tz_as_pytz,
37+
get_utcoffset, get_dst_info,
38+
get_timezone, maybe_get_tz)
3939
from parsing import parse_datetime_string
4040

4141
from nattype import nat_strings, NaT

pandas/_libs/tslibs/fields.pyx

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
3-
# cython: linetrace=False
4-
# distutils: define_macros=CYTHON_TRACE=0
5-
# distutils: define_macros=CYTHON_TRACE_NOGIL=0
63
"""
74
Functions for accessing attributes of Timestamp/datetime64/datetime-like
85
objects and arrays

pandas/_libs/tslibs/parsing.pyx

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
3-
# cython: linetrace=False
4-
# distutils: define_macros=CYTHON_TRACE=0
5-
# distutils: define_macros=CYTHON_TRACE_NOGIL=0
63
"""
74
Parsing functions for datetime and datetime-like strings.
85
"""

pandas/_libs/tslibs/resolution.pyx

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@ np.import_array()
1010

1111
from util cimport is_string_object, get_nat
1212

13-
from pandas._libs.khash cimport (
14-
khiter_t,
15-
kh_destroy_int64, kh_put_int64,
16-
kh_init_int64, kh_int64_t,
17-
kh_resize_int64, kh_get_int64)
13+
from pandas._libs.khash cimport (khiter_t,
14+
kh_destroy_int64, kh_put_int64,
15+
kh_init_int64, kh_int64_t,
16+
kh_resize_int64, kh_get_int64)
1817

1918
from cpython.datetime cimport datetime
2019

2120
from np_datetime cimport (pandas_datetimestruct,
2221
dtstruct_to_dt64, dt64_to_dtstruct)
2322
from frequencies cimport get_freq_code
24-
from timezones cimport (
25-
is_utc, is_tzlocal,
26-
maybe_get_tz, get_dst_info, get_utcoffset)
23+
from timezones cimport (is_utc, is_tzlocal,
24+
maybe_get_tz, get_dst_info, get_utcoffset)
2725
from fields import build_field_sarray
2826
from conversion import tz_convert
2927

pandas/_libs/tslibs/timezones.pyx

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
3-
# cython: linetrace=False
4-
# distutils: define_macros=CYTHON_TRACE=0
5-
# distutils: define_macros=CYTHON_TRACE_NOGIL=0
63

74
cimport cython
85
from cython cimport Py_ssize_t

pandas/tests/indexes/period/test_tools.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import pandas.util.testing as tm
66
import pandas.core.indexes.period as period
77
from pandas.compat import lrange
8-
from pandas.tseries.frequencies import get_freq, MONTHS
8+
from pandas.tseries.frequencies import get_freq
9+
from pandas._libs.tslibs.resolution import _MONTHS as MONTHS
910
from pandas._libs.tslibs.period import period_ordinal, period_asfreq
1011
from pandas import (PeriodIndex, Period, DatetimeIndex, Timestamp, Series,
1112
date_range, to_datetime, period_range)

pandas/tests/test_resample.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
from pandas.core.base import SpecificationError, AbstractMethodError
2323
from pandas.errors import UnsupportedFunctionCall
2424
from pandas.core.groupby import DataError
25-
from pandas._libs.tslibs.resolution import DAYS
26-
from pandas.tseries.frequencies import MONTHS
25+
from pandas._libs.tslibs.resolution import DAYS, _MONTHS as MONTHS
2726
from pandas.tseries.frequencies import to_offset
2827
from pandas.core.indexes.datetimes import date_range
2928
from pandas.tseries.offsets import Minute, BDay

0 commit comments

Comments
 (0)