Skip to content

PERF: cythonizing _concat_date_cols; conversion to float without exceptions in _does_string_look_like_datetime #25754

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 42 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
45dfa46
PERF: rewrited _concat_date_cols function on C with removing extra co…
anmyachev Mar 17, 2019
1531ec9
perf bench for _concat_date_cols
anmyachev Mar 18, 2019
0756da9
Add benchmark for _does_string_look_like_datetime
vnlitvinov Mar 18, 2019
36b8bdb
implemented _does_string_look_like_datetime in cython
anmyachev Mar 19, 2019
a9afbdb
new benchmark for _concat_date_cols func
anmyachev Mar 19, 2019
ee1f32b
init cython version of _concat_date_cols
anmyachev Mar 20, 2019
84e1b00
fix C version of _concat_date_cols
anmyachev Mar 20, 2019
2cf9f22
added ConcatDateColsList benchmark
anmyachev Mar 20, 2019
28fd5f5
ready cython version, combined concat benchmarks
anmyachev Mar 21, 2019
1f17cf9
added forgotten check for float NaN
anmyachev Mar 21, 2019
d1f8ce5
Cython version of _concat_date_cols works for all cases
vnlitvinov Mar 21, 2019
e44212c
Fix typo in _concat_date_cols
vnlitvinov Mar 21, 2019
6af73bf
used flatiter for numpy array
anmyachev Mar 21, 2019
d4305a9
Fix Cython compilation issues
vnlitvinov Mar 22, 2019
fa3ae05
Remove C version of _concat_date_cols
vnlitvinov Mar 22, 2019
49d66e0
Fix linting errors
vnlitvinov Mar 22, 2019
09e4da6
Try to speed up 1D list
vnlitvinov Mar 22, 2019
67d9509
Hopefully speed up 2D case
vnlitvinov Mar 22, 2019
f05564d
Fix isort, retain some comments
vnlitvinov Mar 22, 2019
b9c96fd
removed unnecessary common_include list with headers; some change cod…
anmyachev Mar 25, 2019
6dc3c51
using util.is_array now; changed double to float64_t; fix docstring
anmyachev Mar 27, 2019
08c7f47
split _concat_date_cols functionality
anmyachev Mar 27, 2019
ba6b86a
added error parameter for xstrtod call
anmyachev Mar 28, 2019
14b9cad
removed Py_SIZE; renamed indexes
anmyachev Mar 28, 2019
4e9211b
Switch to helper method for getting C buffer of string object
vnlitvinov Mar 29, 2019
0aefa7b
changed return type in _concat_date_cols_* functions from void to cnp…
anmyachev Mar 31, 2019
a3a0a77
added doc-string to _concat_date_cols* functions
anmyachev Mar 31, 2019
f1ae23c
added doc-string for convert_and_set_item func; removed isinstance(it…
anmyachev Apr 1, 2019
8797e53
fix docstrings
anmyachev Apr 1, 2019
3bdb452
currently only one conversion function is used - convert_to_unicode
anmyachev Apr 5, 2019
dcbcd9a
added some comments in _concat_date_cols_numpy
anmyachev Apr 5, 2019
1d9c7b7
fix problem from rebase
anmyachev Apr 5, 2019
b4fc887
added some comments in _does_string_look_like_datetime
anmyachev Apr 5, 2019
25ee2d2
changed default value of keep_trivial_numbers to true
anmyachev Apr 5, 2019
2046dcb
Remove not needed try..except in _does_string_look_like_datetime benc…
vnlitvinov Apr 29, 2019
28b6670
upgraded doc-ststring; added some blank lines
anmyachev Apr 29, 2019
30f70ab
removed '_concat_date_cols_sequence' func
anmyachev May 6, 2019
3800c40
now only one function '_concat_date_cols'
anmyachev May 6, 2019
b45df3f
removed 'do_convert' local var from 'convert_to_unicode'
anmyachev May 7, 2019
43dffec
replaced '_concat_date_cols' and 'convert_to_unicode' from lib.pyx to…
anmyachev May 7, 2019
c06a662
added 'test_concat_date_col_fail' test
anmyachev May 7, 2019
5dda33c
added doc-string to '_does_string_look_like_datetime' func; changed '…
anmyachev May 9, 2019
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
29 changes: 29 additions & 0 deletions asv_bench/benchmarks/io/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ def time_read_csv(self, infer_datetime_format, format):
infer_datetime_format=infer_datetime_format)


class ReadCSVConcatDatetime(StringIORewind):

iso8601 = '%Y-%m-%d %H:%M:%S'

def setup(self):
rng = date_range('1/1/2000', periods=50000, freq='S')
self.StringIO_input = StringIO('\n'.join(
rng.strftime(self.iso8601).tolist()))

def time_read_csv(self):
read_csv(self.data(self.StringIO_input),
header=None, names=['foo'], parse_dates=['foo'],
infer_datetime_format=False)


class ReadCSVConcatDatetimeBadDateValue(StringIORewind):

params = (['nan', '0', ''],)
param_names = ['bad_date_value']

def setup(self, bad_date_value):
self.StringIO_input = StringIO(('%s,\n' % bad_date_value) * 50000)

def time_read_csv(self, bad_date_value):
read_csv(self.data(self.StringIO_input),
header=None, names=['foo', 'bar'], parse_dates=['foo'],
infer_datetime_format=False)


class ReadCSVSkipRows(BaseIO):

fname = '__test__.csv'
Expand Down
34 changes: 34 additions & 0 deletions asv_bench/benchmarks/io/parsers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np

from pandas._libs.tslibs.parsing import (
_concat_date_cols, _does_string_look_like_datetime)


class DoesStringLookLikeDatetime(object):

params = (['2Q2005', '0.0', '10000'],)
param_names = ['value']

def setup(self, value):
self.objects = [value] * 1000000

def time_check_datetimes(self, value):
for obj in self.objects:
_does_string_look_like_datetime(obj)


class ConcatDateCols(object):

params = ([1234567890, 'AAAA'], [1, 2])
param_names = ['value', 'dim']

def setup(self, value, dim):
count_elem = 10000
if dim == 1:
self.object = (np.array([value] * count_elem),)
if dim == 2:
self.object = (np.array([value] * count_elem),
np.array([value] * count_elem))

def time_check_concat(self, value, dim):
_concat_date_cols(self.object)
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ Performance Improvements
- Improved performance of :meth:`read_csv` by much faster parsing of ``MM/YYYY`` and ``DD/MM/YYYY`` datetime formats (:issue:`25922`)
- Improved performance of nanops for dtypes that cannot store NaNs. Speedup is particularly prominent for :meth:`Series.all` and :meth:`Series.any` (:issue:`25070`)
- Improved performance of :meth:`Series.map` for dictionary mappers on categorical series by mapping the categories instead of mapping all values (:issue:`23785`)
- Improved performance of :meth:`read_csv` by faster concatenating date columns without extra conversion to string for integer/float zero
and float NaN; by faster checking the string for the possibility of being a date (:issue:`25754`)

.. _whatsnew_0250.bug_fixes:

Expand Down
13 changes: 5 additions & 8 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import warnings
import cython
from cython import Py_ssize_t

from cpython cimport (Py_INCREF, PyTuple_SET_ITEM,
PyTuple_New,
Py_EQ,
PyObject_RichCompareBool)
from cpython cimport (Py_INCREF, PyTuple_SET_ITEM, PyTuple_New, PyObject_Str,
Py_EQ, Py_SIZE, PyObject_RichCompareBool,
PyUnicode_Join, PyList_New)

from cpython.datetime cimport (PyDateTime_Check, PyDate_Check,
PyTime_Check, PyDelta_Check,
Expand All @@ -23,10 +22,8 @@ cimport numpy as cnp
from numpy cimport (ndarray, PyArray_GETITEM,
PyArray_ITER_DATA, PyArray_ITER_NEXT, PyArray_IterNew,
flatiter, NPY_OBJECT,
int64_t,
float32_t, float64_t,
uint8_t, uint64_t,
complex128_t)
int64_t, float32_t, float64_t,
uint8_t, uint64_t, complex128_t)
cnp.import_array()

cdef extern from "numpy/arrayobject.h":
Expand Down
181 changes: 168 additions & 13 deletions pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ from io import StringIO

from libc.string cimport strchr

import cython

from cpython cimport PyObject_Str, PyUnicode_Join

from cpython.datetime cimport datetime, datetime_new, import_datetime
from cpython.version cimport PY_VERSION_HEX
import_datetime()

import numpy as np
cimport numpy as cnp
from numpy cimport (PyArray_GETITEM, PyArray_ITER_DATA, PyArray_ITER_NEXT,
PyArray_IterNew, flatiter, float64_t)
cnp.import_array()

# dateutil compat
from dateutil.tz import (tzoffset,
Expand All @@ -26,11 +34,16 @@ from pandas._config import get_option

from pandas._libs.tslibs.ccalendar import MONTH_NUMBERS
from pandas._libs.tslibs.nattype import nat_strings, NaT
from pandas._libs.tslibs.util cimport get_c_string_buf_and_size
from pandas._libs.tslibs.util cimport is_array, get_c_string_buf_and_size

cdef extern from "../src/headers/portable.h":
int getdigit_ascii(char c, int default) nogil

cdef extern from "../src/parser/tokenizer.h":
double xstrtod(const char *p, char **q, char decimal, char sci, char tsep,
int skip_trailing, int *error, int *maybe_int)


# ----------------------------------------------------------------------
# Constants

Expand Down Expand Up @@ -302,20 +315,48 @@ cdef parse_datetime_string_with_reso(date_string, freq=None, dayfirst=False,
return parsed, parsed, reso


cpdef bint _does_string_look_like_datetime(object date_string):
if date_string.startswith('0'):
# Strings starting with 0 are more consistent with a
# date-like string than a number
return True
cpdef bint _does_string_look_like_datetime(object py_string):
"""
Checks whether given string is a datetime: it has to start with '0' or
be greater than 1000.

try:
if float(date_string) < 1000:
return False
except ValueError:
pass
Parameters
----------
py_string: object

if date_string in _not_datelike_strings:
return False
Returns
-------
whether given string is a datetime
"""
cdef:
const char *buf
char *endptr = NULL
Py_ssize_t length = -1
double converted_date
char first
int error = 0

buf = get_c_string_buf_and_size(py_string, &length)
if length >= 1:
first = buf[0]
if first == b'0':
# Strings starting with 0 are more consistent with a
# date-like string than a number
return True
elif py_string in _not_datelike_strings:
return False
else:
# xstrtod with such paramaters copies behavior of python `float`
# cast; for example, " 35.e-1 " is valid string for this cast so,
# for correctly xstrtod call necessary to pass these params:
# b'.' - a dot is used as separator, b'e' - an exponential form of
# a float number can be used, b'\0' - not to use a thousand
# separator, 1 - skip extra spaces before and after,
converted_date = xstrtod(buf, &endptr,
b'.', b'e', b'\0', 1, &error, NULL)
# if there were no errors and the whole line was parsed, then ...
if error == 0 and endptr == buf + length:
return converted_date >= 1000

return True

Expand Down Expand Up @@ -857,3 +898,117 @@ def _guess_datetime_format(dt_str, dayfirst=False, dt_str_parse=du_parse,
return guessed_format
else:
return None


@cython.wraparound(False)
@cython.boundscheck(False)
cdef inline object convert_to_unicode(object item,
bint keep_trivial_numbers):
"""
Convert `item` to str.

Parameters
----------
item : object
keep_trivial_numbers : bool
if True, then conversion (to string from integer/float zero)
is not performed

Returns
-------
str or int or float
"""
cdef:
float64_t float_item

if keep_trivial_numbers:
if isinstance(item, int):
if <int>item == 0:
return item
elif isinstance(item, float):
float_item = item
if float_item == 0.0 or float_item != float_item:
return item

if not isinstance(item, str):
item = PyObject_Str(item)

return item


@cython.wraparound(False)
@cython.boundscheck(False)
def _concat_date_cols(tuple date_cols, bint keep_trivial_numbers=True):
"""
Concatenates elements from numpy arrays in `date_cols` into strings.

Parameters
----------
date_cols : tuple of numpy arrays
keep_trivial_numbers : bool, default True
if True and len(date_cols) == 1, then
conversion (to string from integer/float zero) is not performed

Returns
-------
arr_of_rows : ndarray (dtype=object)

Examples
--------
>>> dates=np.array(['3/31/2019', '4/31/2019'], dtype=object)
>>> times=np.array(['11:20', '10:45'], dtype=object)
>>> result = _concat_date_cols((dates, times))
>>> result
array(['3/31/2019 11:20', '4/31/2019 10:45'], dtype=object)
"""
cdef:
Py_ssize_t rows_count = 0, col_count = len(date_cols)
Py_ssize_t col_idx, row_idx
list list_to_join
cnp.ndarray[object] iters
object[::1] iters_view
flatiter it
cnp.ndarray[object] result
object[:] result_view

if col_count == 0:
return np.zeros(0, dtype=object)

if not all(is_array(array) for array in date_cols):
raise ValueError("not all elements from date_cols are numpy arrays")

rows_count = min(len(array) for array in date_cols)
result = np.zeros(rows_count, dtype=object)
result_view = result

if col_count == 1:
array = date_cols[0]
it = <flatiter>PyArray_IterNew(array)
for row_idx in range(rows_count):
item = PyArray_GETITEM(array, PyArray_ITER_DATA(it))
result_view[row_idx] = convert_to_unicode(item,
keep_trivial_numbers)
PyArray_ITER_NEXT(it)
else:
# create fixed size list - more effecient memory allocation
list_to_join = [None] * col_count
iters = np.zeros(col_count, dtype=object)

# create memoryview of iters ndarray, that will contain some
# flatiter's for each array in `date_cols` - more effecient indexing
iters_view = iters
for col_idx, array in enumerate(date_cols):
iters_view[col_idx] = PyArray_IterNew(array)

# array elements that are on the same line are converted to one string
for row_idx in range(rows_count):
for col_idx, array in enumerate(date_cols):
# this cast is needed, because we did not find a way
# to efficiently store `flatiter` type objects in ndarray
it = <flatiter>iters_view[col_idx]
item = PyArray_GETITEM(array, PyArray_ITER_DATA(it))
list_to_join[col_idx] = convert_to_unicode(item, False)
PyArray_ITER_NEXT(it)
result_view[row_idx] = PyUnicode_Join(' ', list_to_join)

return result
19 changes: 5 additions & 14 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3186,7 +3186,7 @@ def _make_date_converter(date_parser=None, dayfirst=False,
infer_datetime_format=False, cache_dates=True):
def converter(*date_cols):
if date_parser is None:
strs = _concat_date_cols(date_cols)
strs = parsing._concat_date_cols(date_cols)

try:
return tools.to_datetime(
Expand Down Expand Up @@ -3216,10 +3216,10 @@ def converter(*date_cols):
except Exception:
try:
return tools.to_datetime(
parsing.try_parse_dates(_concat_date_cols(date_cols),
parser=date_parser,
dayfirst=dayfirst),
cache=cache_dates,
parsing.try_parse_dates(
parsing._concat_date_cols(date_cols),
parser=date_parser,
dayfirst=dayfirst),
errors='ignore')
except Exception:
return generic_parser(date_parser, *date_cols)
Expand Down Expand Up @@ -3511,15 +3511,6 @@ def _get_col_names(colspec, columns):
return colnames


def _concat_date_cols(date_cols):
if len(date_cols) == 1:
return np.array([str(x) for x in date_cols[0]], dtype=object)

rs = np.array([' '.join(str(y) for y in x)
for x in zip(*date_cols)], dtype=object)
return rs


class FixedWidthReader(BaseIterator):
"""
A reader of fixed-width lines.
Expand Down
Loading