Skip to content

Commit 604fc73

Browse files
committed
now only one function '_concat_date_cols'
1 parent 19f9d7d commit 604fc73

File tree

2 files changed

+20
-52
lines changed

2 files changed

+20
-52
lines changed

asv_bench/benchmarks/io/parsers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ConcatDateCols(object):
2424
param_names = ['value', 'dim']
2525

2626
def setup(self, value, dim):
27-
count_elem = 100000
27+
count_elem = 10000
2828
if dim == 1:
2929
self.object = (np.array([value] * count_elem),)
3030
if dim == 2:

pandas/_libs/lib.pyx

+19-51
Original file line numberDiff line numberDiff line change
@@ -2352,34 +2352,31 @@ cdef inline object convert_to_unicode(object item,
23522352

23532353
@cython.wraparound(False)
23542354
@cython.boundscheck(False)
2355-
cdef cnp.ndarray[object] _concat_date_cols_numpy(tuple date_cols,
2356-
Py_ssize_t rows_count,
2357-
Py_ssize_t col_count,
2358-
bint keep_trivial_numbers):
2355+
def _concat_date_cols(tuple date_cols, bint keep_trivial_numbers=True):
23592356
"""
2360-
Concatenates elements from numpy arrays into strings.
2357+
Concatenates elements from numpy arrays in `date_cols` into strings.
23612358
23622359
Parameters
23632360
----------
23642361
date_cols : tuple of numpy arrays
2365-
rows_count : Py_ssize_t
2366-
count of elements from arrays that will be concatenated
2367-
col_count : Py_ssize_t
2368-
count of arrays whose elements will be concatenated
2369-
keep_trivial_numbers : bool, default False
2362+
keep_trivial_numbers : bool, default True
23702363
if True and len(date_cols) == 1, then
23712364
conversion (to string from integer/float zero) is not performed
23722365
23732366
Returns
23742367
-------
23752368
arr_of_rows : ndarray (dtype=object)
23762369
2377-
Notes
2378-
-----
2379-
This function speeds up concatenation for numpy arrays.
2380-
You also can use `_concat_date_cols_sequence` function.
2370+
Examples
2371+
--------
2372+
>>> dates=np.array(['3/31/2019', '4/31/2019'], dtype=object)
2373+
>>> times=np.array(['11:20', '10:45'], dtype=object)
2374+
>>> result = _concat_date_cols((dates, times))
2375+
>>> result
2376+
array(['3/31/2019 11:20', '4/31/2019 10:45'], dtype=object)
23812377
"""
23822378
cdef:
2379+
Py_ssize_t rows_count = 0, col_count = len(date_cols)
23832380
Py_ssize_t col_idx, row_idx
23842381
list list_to_join
23852382
cnp.ndarray[object] iters
@@ -2388,6 +2385,14 @@ cdef cnp.ndarray[object] _concat_date_cols_numpy(tuple date_cols,
23882385
cnp.ndarray[object] result
23892386
object[:] result_view
23902387

2388+
if col_count == 0:
2389+
return np.zeros(0, dtype=object)
2390+
2391+
2392+
if not all(util.is_array(array) for array in date_cols):
2393+
raise ValueError("not all elements from date_cols are numpy arrays")
2394+
2395+
rows_count = min(len(array) for array in date_cols)
23912396
result = np.zeros(rows_count, dtype=object)
23922397
result_view = result
23932398

@@ -2422,40 +2427,3 @@ cdef cnp.ndarray[object] _concat_date_cols_numpy(tuple date_cols,
24222427
result_view[row_idx] = PyUnicode_Join(' ', list_to_join)
24232428

24242429
return result
2425-
2426-
2427-
def _concat_date_cols(tuple date_cols, bint keep_trivial_numbers=True):
2428-
"""
2429-
Concatenates elements from numpy arrays in `date_cols` into strings.
2430-
2431-
Parameters
2432-
----------
2433-
date_cols : tuple of sequences
2434-
keep_trivial_numbers : bool, default True
2435-
if True and len(date_cols) == 1, then
2436-
conversion (to string from integer/float zero) is not performed
2437-
2438-
Returns
2439-
-------
2440-
arr_of_rows : ndarray (dtype=object)
2441-
2442-
Examples
2443-
--------
2444-
>>> dates=np.array(['3/31/2019', '4/31/2019'], dtype=object)
2445-
>>> times=np.array(['11:20', '10:45'], dtype=object)
2446-
>>> result = _concat_date_cols((dates, times))
2447-
>>> result
2448-
array(['3/31/2019 11:20', '4/31/2019 10:45'], dtype=object)
2449-
"""
2450-
cdef:
2451-
Py_ssize_t rows_count = 0, col_count = len(date_cols)
2452-
2453-
if col_count == 0:
2454-
return np.zeros(0, dtype=object)
2455-
2456-
rows_count = min(len(array) for array in date_cols)
2457-
2458-
if all(util.is_array(array) for array in date_cols):
2459-
return _concat_date_cols_numpy(date_cols, rows_count, col_count,
2460-
keep_trivial_numbers)
2461-
raise ValueError("not all elements from date_cols are numpy arrays")

0 commit comments

Comments
 (0)