File tree 5 files changed +40
-9
lines changed 5 files changed +40
-9
lines changed Original file line number Diff line number Diff line change @@ -122,7 +122,7 @@ pandas 0.11.0
122
122
- Support null checking on timedelta64, representing (and formatting) with NaT
123
123
- Support setitem with np.nan value, converts to NaT
124
124
- Support min/max ops in a Dataframe (abs not working, nor do we error on non-supported ops)
125
- - Support idxmin/idxmax in a Series (but with no NaT)
125
+ - Support idxmin/idxmax/abs in a Series (but with no NaT)
126
126
127
127
- Bug on in-place putmasking on an ``integer `` series that needs to be converted to ``float `` (GH2746 _)
128
128
- Bug in argsort of ``datetime64[ns] `` Series with ``NaT `` (GH2967 _)
Original file line number Diff line number Diff line change @@ -916,7 +916,7 @@ def _possibly_convert_platform(values):
916
916
return values
917
917
918
918
919
- def _possibly_cast_to_timedelta (value ):
919
+ def _possibly_cast_to_timedelta (value , coerce = True ):
920
920
""" try to cast to timedelta64 w/o coercion """
921
921
922
922
# deal with numpy not being able to handle certain timedelta operations
@@ -925,9 +925,12 @@ def _possibly_cast_to_timedelta(value):
925
925
value = value .astype ('timedelta64[ns]' )
926
926
return value
927
927
928
- new_value = tslib .array_to_timedelta64 (value .astype (object ), coerce = False )
929
- if new_value .dtype == 'i8' :
930
- value = np .array (new_value ,dtype = 'timedelta64[ns]' )
928
+ # we don't have a timedelta, but we want to try to convert to one (but don't force it)
929
+ if coerce :
930
+ new_value = tslib .array_to_timedelta64 (value .astype (object ), coerce = False )
931
+ if new_value .dtype == 'i8' :
932
+ value = np .array (new_value ,dtype = 'timedelta64[ns]' )
933
+
931
934
return value
932
935
933
936
def _possibly_cast_to_datetime (value , dtype , coerce = False ):
Original file line number Diff line number Diff line change @@ -711,6 +711,19 @@ def mask(self, cond):
711
711
"""
712
712
return self .where (~ cond , nan )
713
713
714
+ def abs (self ):
715
+ """
716
+ Return an object with absolute value taken. Only applicable to objects
717
+ that are all numeric
718
+
719
+ Returns
720
+ -------
721
+ abs: type of caller
722
+ """
723
+ obj = np .abs (self )
724
+ obj = com ._possibly_cast_to_timedelta (obj , coerce = False )
725
+ return obj
726
+
714
727
def __setitem__ (self , key , value ):
715
728
try :
716
729
try :
Original file line number Diff line number Diff line change @@ -2913,10 +2913,11 @@ def test_operators_timedelta64(self):
2913
2913
self .assert_ ((result == diffs ['A' ]).all () == True )
2914
2914
2915
2915
# abs ###### THIS IS BROKEN NOW ###### (results are dtype=timedelta64[us]
2916
- result = np .abs (df ['A' ]- df ['B' ])
2917
- result = diffs .abs ()
2918
- expected = DataFrame (dict (A = df ['A' ]- df ['C' ],
2919
- B = df ['B' ]- df ['A' ]))
2916
+ # even though fixed in series
2917
+ #result = np.abs(df['A']-df['B'])
2918
+ #result = diffs.abs()
2919
+ #expected = DataFrame(dict(A = df['A']-df['C'],
2920
+ # B = df['B']-df['A']))
2920
2921
#assert_frame_equal(result,expected)
2921
2922
2922
2923
# mixed frame
Original file line number Diff line number Diff line change @@ -1801,9 +1801,11 @@ def test_operators_timedelta64(self):
1801
1801
self .assert_ (result .dtype == 'm8[ns]' )
1802
1802
assert_series_equal (result ,expected )
1803
1803
1804
+
1804
1805
def test_timedelta64_functions (self ):
1805
1806
1806
1807
from datetime import timedelta
1808
+ from pandas import date_range
1807
1809
1808
1810
# index min/max
1809
1811
td = Series (date_range ('2012-1-1' , periods = 3 , freq = 'D' ))- Timestamp ('20120101' )
@@ -1823,6 +1825,18 @@ def test_timedelta64_functions(self):
1823
1825
#result = td.idxmax()
1824
1826
#self.assert_(result == 2)
1825
1827
1828
+ # abs
1829
+ s1 = Series (date_range ('20120101' ,periods = 3 ))
1830
+ s2 = Series (date_range ('20120102' ,periods = 3 ))
1831
+ expected = Series (s2 - s1 )
1832
+
1833
+ # this fails as numpy returns timedelta64[us]
1834
+ #result = np.abs(s1-s2)
1835
+ #assert_frame_equal(result,expected)
1836
+
1837
+ result = (s1 - s2 ).abs ()
1838
+ assert_series_equal (result ,expected )
1839
+
1826
1840
def test_sub_of_datetime_from_TimeSeries (self ):
1827
1841
from pandas .core import common as com
1828
1842
from datetime import datetime
You can’t perform that action at this time.
0 commit comments