@@ -930,89 +930,103 @@ They can be both positive and negative.
930
930
931
931
.. ipython :: python
932
932
933
- from datetime import datetime, timedelta
934
- s = Series(date_range(' 2012-1-1' , periods = 3 , freq = ' D' ))
935
- td = Series([ timedelta(days = i) for i in range (3 ) ])
936
- df = DataFrame(dict (A = s, B = td))
937
- df
938
- df[' C' ] = df[' A' ] + df[' B' ]
939
- df
940
- df.dtypes
941
-
942
- s - s.max()
943
- s - datetime(2011 ,1 ,1 ,3 ,5 )
944
- s + timedelta(minutes = 5 )
933
+ from datetime import datetime, timedelta
934
+ s = Series(date_range(' 2012-1-1' , periods = 3 , freq = ' D' ))
935
+ td = Series([ timedelta(days = i) for i in range (3 ) ])
936
+ df = DataFrame(dict (A = s, B = td))
937
+ df
938
+ df[' C' ] = df[' A' ] + df[' B' ]
939
+ df
940
+ df.dtypes
941
+
942
+ s - s.max()
943
+ s - datetime(2011 ,1 ,1 ,3 ,5 )
944
+ s + timedelta(minutes = 5 )
945
945
946
946
Getting scalar results from a ``timedelta64[ns] `` series
947
947
948
+ .. ipython :: python
949
+ :suppress:
950
+
951
+ from distutils.version import LooseVersion
952
+
948
953
.. ipython :: python
949
954
950
955
y = s - s[0 ]
951
956
y
952
- y.apply(lambda x : x.item().total_seconds())
953
- y.apply(lambda x : x.item().days)
954
-
955
- .. note ::
956
957
957
- These operations are different in numpy 1.6.2 and in numpy >= 1.7. The ``timedelta64[ns] `` scalar
958
- type in 1.6.2 is much like a ``datetime.timedelta ``, while in 1.7 it is a nanosecond based integer.
959
- A future version of pandas will make this transparent.
958
+ if LooseVersion(np.__version__ ) <= ' 1.6.2' :
959
+ y.apply(lambda x : x.item().total_seconds())
960
+ y.apply(lambda x : x.item().days)
961
+ else :
962
+ y.apply(lambda x : x / np.timedelta64(1 , ' s' ))
963
+ y.apply(lambda x : x / np.timedelta64(1 , ' D' ))
964
+
965
+ .. note ::
960
966
961
- These are the equivalent operation to above in numpy >= 1.7
967
+ As you can see from the conditional statement above, these operations are
968
+ different in numpy 1.6.2 and in numpy >= 1.7. The ``timedelta64[ns] `` scalar
969
+ type in 1.6.2 is much like a ``datetime.timedelta ``, while in 1.7 it is a
970
+ nanosecond based integer. A future version of pandas will make this
971
+ transparent.
962
972
963
- `` y.apply(lambda x: x.item()/np.timedelta64(1,'s')) ``
973
+ .. note ::
964
974
965
- ``y.apply(lambda x: x.item()/np.timedelta64(1,'D')) ``
975
+ In numpy >= 1.7 dividing a ``timedelta64 `` array by another ``timedelta64 ``
976
+ array will yield an array with dtype ``np.float64 ``.
966
977
967
978
Series of timedeltas with ``NaT `` values are supported
968
979
969
980
.. ipython :: python
970
981
971
- y = s - s.shift()
972
- y
982
+ y = s - s.shift()
983
+ y
984
+
973
985
The can be set to ``NaT `` using ``np.nan `` analagously to datetimes
974
986
975
987
.. ipython :: python
976
988
977
- y[1 ] = np.nan
978
- y
989
+ y[1 ] = np.nan
990
+ y
979
991
980
992
Operands can also appear in a reversed order (a singluar object operated with a Series)
981
993
982
994
.. ipython :: python
983
995
984
- s.max() - s
985
- datetime(2011 ,1 ,1 ,3 ,5 ) - s
986
- timedelta(minutes = 5 ) + s
996
+ s.max() - s
997
+ datetime(2011 ,1 ,1 ,3 ,5 ) - s
998
+ timedelta(minutes = 5 ) + s
987
999
988
1000
Some timedelta numeric like operations are supported.
989
1001
990
1002
.. ipython :: python
991
1003
992
- td - timedelta(minutes = 5 ,seconds = 5 ,microseconds = 5 )
1004
+ td - timedelta(minutes = 5 , seconds = 5 , microseconds = 5 )
993
1005
994
1006
``min, max `` and the corresponding ``idxmin, idxmax `` operations are support on frames
995
1007
996
1008
.. ipython :: python
997
1009
998
- df = DataFrame(dict (A = s - Timestamp(' 20120101' )- timedelta(minutes = 5 ,seconds = 5 ),
999
- B = s - Series(date_range(' 2012-1-2' , periods = 3 , freq = ' D' ))))
1000
- df
1010
+ A = s - Timestamp(' 20120101' ) - timedelta(minutes = 5 , seconds = 5 )
1011
+ B = s - Series(date_range(' 2012-1-2' , periods = 3 , freq = ' D' ))
1001
1012
1013
+ df = DataFrame(dict (A = A, B = B))
1014
+ df
1002
1015
1003
- df.min()
1004
- df.min(axis = 1 )
1016
+ df.min()
1017
+ df.min(axis = 1 )
1005
1018
1006
- df.idxmin()
1007
- df.idxmax()
1019
+ df.idxmin()
1020
+ df.idxmax()
1008
1021
1009
- ``min, max `` operations are support on series, these return a single element ``timedelta64[ns] `` Series (this avoids
1010
- having to deal with numpy timedelta64 issues). ``idxmin, idxmax `` are supported as well.
1022
+ ``min, max `` operations are support on series, these return a single element
1023
+ ``timedelta64[ns] `` Series (this avoids having to deal with numpy timedelta64
1024
+ issues). ``idxmin, idxmax `` are supported as well.
1011
1025
1012
1026
.. ipython :: python
1013
1027
1014
- df.min().max()
1015
- df.min(axis = 1 ).min()
1028
+ df.min().max()
1029
+ df.min(axis = 1 ).min()
1016
1030
1017
- df.min().idxmax()
1018
- df.min(axis = 1 ).idxmin()
1031
+ df.min().idxmax()
1032
+ df.min(axis = 1 ).idxmin()
0 commit comments