Skip to content

Commit 392a377

Browse files
committed
simplify concat
1 parent 54cfac5 commit 392a377

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

pandas/core/dtypes/concat.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ def _concat_datetime(to_concat, axis=0, typs=None):
432432
a single array, preserving the combined dtypes
433433
"""
434434

435+
def concatenate(to_concat, axis):
436+
# coerce to 2d if needed & concatenate
437+
if axis == 1:
438+
to_concat = [np.atleast_2d(x) for x in to_concat]
439+
return np.concatenate(to_concat, axis=axis)
440+
435441
def convert_to_pydatetime(x, axis):
436442
# coerce to an object dtype
437443

@@ -450,8 +456,6 @@ def convert_to_pydatetime(x, axis):
450456
x = tslib.ints_to_pytimedelta(x.view(np.int64).ravel(), box=True)
451457
x = x.reshape(shape)
452458

453-
if axis == 1:
454-
x = np.atleast_2d(x)
455459
return x
456460

457461
if typs is None:
@@ -467,20 +471,15 @@ def convert_to_pydatetime(x, axis):
467471
if 'datetime' in typs:
468472
to_concat = [np.array(x, copy=False).view(np.int64)
469473
for x in to_concat]
470-
if axis == 1:
471-
to_concat = [np.atleast_2d(x) for x in to_concat]
472-
473-
new_values = np.concatenate(to_concat, axis=axis)
474-
return new_values.view(_NS_DTYPE)
474+
return concatenate(to_concat, axis=axis).view(_NS_DTYPE)
475475
else:
476476
# when to_concat has different tz, len(typs) > 1.
477477
# thus no need to care
478478
return _concat_datetimetz(to_concat)
479479

480480
elif 'timedelta' in typs:
481-
new_values = np.concatenate([x.view(np.int64) for x in to_concat],
482-
axis=axis)
483-
return new_values.view(_TD_DTYPE)
481+
return concatenate([x.view(np.int64) for x in to_concat],
482+
axis=axis).view(_TD_DTYPE)
484483

485484
elif _contains_period:
486485
# PeriodIndex must be handled by PeriodIndex,
@@ -489,8 +488,8 @@ def convert_to_pydatetime(x, axis):
489488
raise NotImplementedError
490489

491490
# need to coerce to object
492-
to_concat = [convert_to_pydatetime(x, axis) for x in to_concat]
493-
return np.concatenate(to_concat, axis=axis)
491+
return concatenate([convert_to_pydatetime(x, axis) for x in to_concat],
492+
axis=axis)
494493

495494

496495
def _concat_datetimetz(to_concat, name=None):

0 commit comments

Comments
 (0)