Skip to content

Commit ec0e61a

Browse files
committed
BUG: upconvert datetime64 to datetime.datetime when necessary in concat steps. close #2624
1 parent ee9b2b6 commit ec0e61a

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pandas 0.10.1
100100
- Don't lose time zone when calling DatetimeIndex.drop (GH2621_)
101101
- Fix setitem on a Series with a boolean key and a non-scalar as value (GH2686_)
102102
- Box datetime64 values in Series.apply/map (GH2627_, GH2689_)
103+
- Upconvert datetime + datetime64 values when concatenating frames (GH2624_)
103104

104105
**API Changes**
105106

@@ -120,6 +121,7 @@ pandas 0.10.1
120121
.. _GH2613: https://github.com/pydata/pandas/issues/2613
121122
.. _GH2616: https://github.com/pydata/pandas/issues/2616
122123
.. _GH2621: https://github.com/pydata/pandas/issues/2621
124+
.. _GH2624: https://github.com/pydata/pandas/issues/2624
123125
.. _GH2625: https://github.com/pydata/pandas/issues/2625
124126
.. _GH2627: https://github.com/pydata/pandas/issues/2627
125127
.. _GH2643: https://github.com/pydata/pandas/issues/2643

pandas/core/common.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,13 +1216,26 @@ def _concat_compat(to_concat, axis=0):
12161216
# filter empty arrays
12171217
to_concat = [x for x in to_concat if x.shape[axis] > 0]
12181218

1219-
if all(x.dtype == _NS_DTYPE for x in to_concat):
1219+
is_datetime64 = [x.dtype == _NS_DTYPE for x in to_concat]
1220+
if all(is_datetime64):
12201221
# work around NumPy 1.6 bug
12211222
new_values = np.concatenate([x.view(np.int64) for x in to_concat],
12221223
axis=axis)
12231224
return new_values.view(_NS_DTYPE)
1224-
else:
1225-
return np.concatenate(to_concat, axis=axis)
1225+
elif any(is_datetime64):
1226+
to_concat = [_to_pydatetime(x) for x in to_concat]
1227+
1228+
return np.concatenate(to_concat, axis=axis)
1229+
1230+
1231+
def _to_pydatetime(x):
1232+
if x.dtype == _NS_DTYPE:
1233+
shape = x.shape
1234+
x = tslib.ints_to_pydatetime(x.view(np.int64).ravel())
1235+
x = x.reshape(shape)
1236+
1237+
return x
1238+
12261239

12271240
def _where_compat(mask, arr1, arr2):
12281241
if arr1.dtype == _NS_DTYPE and arr2.dtype == _NS_DTYPE:

pandas/tseries/tests/test_timeseries.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import pandas.core.datetools as datetools
1919
import pandas.tseries.offsets as offsets
2020
import pandas.tseries.frequencies as fmod
21+
import pandas as pd
2122

2223
from pandas.util.testing import assert_series_equal, assert_almost_equal
2324
import pandas.util.testing as tm
@@ -1315,7 +1316,6 @@ def test_frame_datetime64_handling_groupby(self):
13151316

13161317
def test_series_interpolate_intraday(self):
13171318
# #1698
1318-
import pandas as pd
13191319
index = pd.date_range('1/1/2012', periods=4, freq='12D')
13201320
ts = pd.Series([0, 12, 24, 36], index)
13211321
new_index = index.append(index + pd.DateOffset(days=1)).order()
@@ -1409,6 +1409,21 @@ def f(x):
14091409
s.apply(f)
14101410
DataFrame(s).applymap(f)
14111411

1412+
def test_concat_datetime_datetime64_frame(self):
1413+
# #2624
1414+
rows = []
1415+
rows.append([datetime(2010, 1, 1), 1])
1416+
rows.append([datetime(2010, 1, 2), 'hi'])
1417+
1418+
df2_obj = DataFrame.from_records(rows, columns=['date', 'test'])
1419+
1420+
ind = date_range(start="2000/1/1", freq="D", periods=10)
1421+
df1 = DataFrame({'date': ind, 'test':range(10)})
1422+
1423+
# it works!
1424+
pd.concat([df1, df2_obj])
1425+
1426+
14121427
def _simple_ts(start, end, freq='D'):
14131428
rng = date_range(start, end, freq=freq)
14141429
return Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)