@@ -1887,65 +1887,68 @@ def _maybe_box_datetimelike(value):
18871887
18881888_values_from_object = lib .values_from_object
18891889
1890- def _possibly_convert_objects (values , convert_dates = True ,
1891- convert_numeric = True ,
1892- convert_timedeltas = True ):
1890+
1891+ def _possibly_convert_objects (values ,
1892+ datetime = True ,
1893+ numeric = True ,
1894+ timedelta = True ,
1895+ coerce = False ,
1896+ copy = True ):
18931897 """ if we have an object dtype, try to coerce dates and/or numbers """
18941898
1895- # if we have passed in a list or scalar
1899+ conversion_count = sum ((datetime , numeric , timedelta ))
1900+ if conversion_count == 0 :
1901+ import warnings
1902+ warnings .warn ('Must explicitly pass type for conversion. Defaulting to '
1903+ 'pre-0.17 behavior where datetime=True, numeric=True, '
1904+ 'timedelta=True and coerce=False' , DeprecationWarning )
1905+ datetime = numeric = timedelta = True
1906+ coerce = False
1907+
18961908 if isinstance (values , (list , tuple )):
1909+ # List or scalar
18971910 values = np .array (values , dtype = np .object_ )
1898- if not hasattr (values , 'dtype' ):
1911+ elif not hasattr (values , 'dtype' ):
18991912 values = np .array ([values ], dtype = np .object_ )
1900-
1901- # convert dates
1902- if convert_dates and values .dtype == np .object_ :
1903-
1904- # we take an aggressive stance and convert to datetime64[ns]
1905- if convert_dates == 'coerce' :
1906- new_values = _possibly_cast_to_datetime (
1907- values , 'M8[ns]' , coerce = True )
1908-
1909- # if we are all nans then leave me alone
1910- if not isnull (new_values ).all ():
1911- values = new_values
1912-
1913- else :
1914- values = lib .maybe_convert_objects (
1915- values , convert_datetime = convert_dates )
1916-
1917- # convert timedeltas
1918- if convert_timedeltas and values .dtype == np .object_ :
1919-
1920- if convert_timedeltas == 'coerce' :
1921- from pandas .tseries .timedeltas import to_timedelta
1922- values = to_timedelta (values , coerce = True )
1923-
1924- # if we are all nans then leave me alone
1925- if not isnull (new_values ).all ():
1926- values = new_values
1927-
1928- else :
1929- values = lib .maybe_convert_objects (
1930- values , convert_timedelta = convert_timedeltas )
1931-
1932- # convert to numeric
1933- if values .dtype == np .object_ :
1934- if convert_numeric :
1935- try :
1936- new_values = lib .maybe_convert_numeric (
1937- values , set (), coerce_numeric = True )
1938-
1939- # if we are all nans then leave me alone
1940- if not isnull (new_values ).all ():
1941- values = new_values
1942-
1943- except :
1944- pass
1945- else :
1946-
1947- # soft-conversion
1948- values = lib .maybe_convert_objects (values )
1913+ elif not is_object_dtype (values .dtype ):
1914+ # If not object, do not attempt conversion
1915+ values = values .copy () if copy else values
1916+ return values
1917+
1918+ # If 1 flag is coerce, ensure 2 others are False
1919+ if coerce :
1920+ if conversion_count > 1 :
1921+ raise ValueError ("Only one of 'datetime', 'numeric' or "
1922+ "'timedelta' can be True when when coerce=True." )
1923+
1924+ # Immediate return if coerce
1925+ if datetime :
1926+ return pd .to_datetime (values , coerce = True , box = False )
1927+ elif timedelta :
1928+ return pd .to_timedelta (values , coerce = True , box = False )
1929+ elif numeric :
1930+ return lib .maybe_convert_numeric (values , set (), coerce_numeric = True )
1931+
1932+ # Soft conversions
1933+ if datetime :
1934+ values = lib .maybe_convert_objects (values ,
1935+ convert_datetime = datetime )
1936+
1937+ if timedelta and is_object_dtype (values .dtype ):
1938+ # Object check to ensure only run if previous did not convert
1939+ values = lib .maybe_convert_objects (values ,
1940+ convert_timedelta = timedelta )
1941+
1942+ if numeric and is_object_dtype (values .dtype ):
1943+ try :
1944+ converted = lib .maybe_convert_numeric (values ,
1945+ set (),
1946+ coerce_numeric = True )
1947+ # If all NaNs, then do not-alter
1948+ values = converted if not isnull (converted ).all () else values
1949+ values = values .copy () if copy else values
1950+ except :
1951+ pass
19491952
19501953 return values
19511954
0 commit comments