Skip to content

Commit b0a19c8

Browse files
committed
BUG: don't convert object->datetime64 when calling DataFrame.apply. close #2374
1 parent d010899 commit b0a19c8

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ pandas 0.10.0
138138
- Fixed issue with missing attributes after loading a pickled dataframe (#2431)
139139
- Fix Timestamp formatting with tzoffset time zone in dateutil 2.1 (#2443)
140140
- Fix GroupBy.apply issue when using BinGrouper to do ts binning (#2300)
141+
- Fix issues resulting from datetime.datetime columns being converted to
142+
datetime64 when calling DataFrame.apply. (#2374)
143+
141144

142145
pandas 0.9.1
143146
============

pandas/core/frame.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ def info(self, verbose=True, buf=None):
15941594
def dtypes(self):
15951595
return self.apply(lambda x: x.dtype)
15961596

1597-
def convert_objects(self):
1597+
def convert_objects(self, convert_dates=True):
15981598
"""
15991599
Attempt to infer better dtype for object columns
16001600
@@ -1603,7 +1603,8 @@ def convert_objects(self):
16031603
converted : DataFrame
16041604
"""
16051605
new_data = {}
1606-
convert_f = lambda x: lib.maybe_convert_objects(x, convert_datetime=1)
1606+
convert_f = lambda x: lib.maybe_convert_objects(
1607+
x, convert_datetime=convert_dates)
16071608

16081609
# TODO: could be more efficient taking advantage of the block
16091610
for col, s in self.iteritems():
@@ -4123,7 +4124,7 @@ def _apply_standard(self, func, axis, ignore_failures=False):
41234124

41244125
if axis == 1:
41254126
result = result.T
4126-
result = result.convert_objects()
4127+
result = result.convert_objects(convert_dates=False)
41274128

41284129
return result
41294130
else:

pandas/core/series.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ def astype(self, dtype):
745745
casted = com._astype_nansafe(self.values, dtype)
746746
return self._constructor(casted, index=self.index, name=self.name)
747747

748-
def convert_objects(self):
748+
def convert_objects(self, convert_dates=True):
749749
"""
750750
Attempt to infer better dtype
751751
@@ -754,8 +754,8 @@ def convert_objects(self):
754754
converted : Series
755755
"""
756756
if self.dtype == np.object_:
757-
return Series(lib.maybe_convert_objects(self, convert_datetime=1),
758-
self.index)
757+
return Series(lib.maybe_convert_objects(
758+
self, convert_datetime=convert_dates), self.index)
759759
return self
760760

761761
def repeat(self, reps):

pandas/sparse/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def _consolidate_inplace(self):
114114
# do nothing when DataFrame calls this method
115115
pass
116116

117-
def convert_objects(self):
117+
def convert_objects(self, convert_dates=True):
118118
# XXX
119119
return self
120120

pandas/tseries/tests/test_timeseries.py

+8
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,14 @@ def test_get_level_values_box(self):
20552055

20562056
self.assertTrue(isinstance(index.get_level_values(0)[0], Timestamp))
20572057

2058+
def test_frame_apply_dont_convert_datetime64(self):
2059+
from pandas.tseries.offsets import BDay
2060+
df = DataFrame({'x1': [datetime(1996,1,1)]})
2061+
df = df.applymap(lambda x: x+BDay())
2062+
df = df.applymap(lambda x: x+BDay())
2063+
2064+
self.assertTrue(df.x1.dtype == object)
2065+
20582066

20592067
class TestLegacyCompat(unittest.TestCase):
20602068

0 commit comments

Comments
 (0)