Skip to content

Commit 9f0094f

Browse files
committed
BUG: timedelta64 merge/join issues (GH5695)
1 parent 58ed629 commit 9f0094f

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

pandas/tools/merge.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pandas.core.index import (Index, MultiIndex, _get_combined_index,
1515
_ensure_index, _get_consensus_names,
1616
_all_indexes_same)
17-
from pandas.core.internals import (IntBlock, BoolBlock, BlockManager,
17+
from pandas.core.internals import (TimeDeltaBlock, IntBlock, BoolBlock, BlockManager,
1818
make_block, _consolidate)
1919
from pandas.util.decorators import cache_readonly, Appender, Substitution
2020
from pandas.core.common import (PandasError, ABCSeries,
@@ -816,7 +816,7 @@ def reindex_block(self, block, axis, ref_items, copy=True):
816816

817817
def _may_need_upcasting(blocks):
818818
for block in blocks:
819-
if isinstance(block, (IntBlock, BoolBlock)):
819+
if isinstance(block, (IntBlock, BoolBlock)) and not isinstance(block, TimeDeltaBlock):
820820
return True
821821
return False
822822

@@ -827,7 +827,10 @@ def _upcast_blocks(blocks):
827827
"""
828828
new_blocks = []
829829
for block in blocks:
830-
if isinstance(block, IntBlock):
830+
if isinstance(block, TimeDeltaBlock):
831+
# these are int blocks underlying, but are ok
832+
newb = block
833+
elif isinstance(block, IntBlock):
831834
newb = make_block(block.values.astype(float), block.items,
832835
block.ref_items, placement=block._ref_locs)
833836
elif isinstance(block, BoolBlock):

pandas/tools/tests/test_merge.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,26 @@ def test_append_dtype_coerce(self):
791791
result = df1.append(df2,ignore_index=True)
792792
assert_frame_equal(result, expected)
793793

794+
# timedelta64
795+
# GH 5695
796+
d = {'d': dt.datetime(2013, 11, 5, 5, 56), 't': dt.timedelta(0, 22500)}
797+
df = DataFrame(columns=list('dt'))
798+
df = df.append(d, ignore_index=True)
799+
result = df.append(d, ignore_index=True)
800+
expected = DataFrame({'d': [dt.datetime(2013, 11, 5, 5, 56),
801+
dt.datetime(2013, 11, 5, 5, 56) ],
802+
't': [ dt.timedelta(0, 22500),
803+
dt.timedelta(0, 22500) ]})
804+
assert_frame_equal(result, expected)
805+
806+
td = np.timedelta64(300000000)
807+
lhs = DataFrame(Series([td,td],index=["A","B"]))
808+
rhs = DataFrame(Series([td],index=["A"]))
809+
810+
from pandas import NaT
811+
result = lhs.join(rhs,rsuffix='r', how="left")
812+
expected = DataFrame({ '0' : Series([td,td],index=list('AB')), '0r' : Series([td,NaT],index=list('AB')) })
813+
assert_frame_equal(result, expected)
794814

795815
def test_overlapping_columns_error_message(self):
796816
# #2649
@@ -1763,7 +1783,19 @@ def test_concat_datetime64_block(self):
17631783
df = DataFrame({'time': rng})
17641784

17651785
result = concat([df, df])
1766-
self.assert_((result[:10]['time'] == rng).all())
1786+
self.assert_((result.iloc[:10]['time'] == rng).all())
1787+
self.assert_((result.iloc[10:]['time'] == rng).all())
1788+
1789+
def test_concat_timedelta64_block(self):
1790+
from pandas import to_timedelta
1791+
1792+
rng = to_timedelta(np.arange(10),unit='s')
1793+
1794+
df = DataFrame({'time': rng})
1795+
1796+
result = concat([df, df])
1797+
self.assert_((result.iloc[:10]['time'] == rng).all())
1798+
self.assert_((result.iloc[10:]['time'] == rng).all())
17671799

17681800
def test_concat_keys_with_none(self):
17691801
# #1649

0 commit comments

Comments
 (0)