1212 _NS_DTYPE , _TD_DTYPE , ABCSeries , is_list_like ,
1313 ABCSparseSeries , _infer_dtype_from_scalar ,
1414 _is_null_datelike_scalar ,
15- is_timedelta64_dtype , is_datetime64_dtype ,)
15+ is_timedelta64_dtype , is_datetime64_dtype ,
16+ _possibly_infer_to_datetimelike )
1617from pandas .core .index import Index , MultiIndex , _ensure_index
1718from pandas .core .indexing import (_maybe_convert_indices , _length_of_indexer )
1819import pandas .core .common as com
@@ -1807,26 +1808,21 @@ def make_block(values, placement, klass=None, ndim=None,
18071808 elif issubclass (vtype , np .complexfloating ):
18081809 klass = ComplexBlock
18091810
1810- # try to infer a DatetimeBlock, or set to an ObjectBlock
18111811 else :
18121812
1813+ # we want to infer here if its a datetimelike if its object type
1814+ # this is pretty strict in that it requires a datetime/timedelta
1815+ # value IN addition to possible nulls/strings
1816+ # an array of ONLY strings will not be inferred
18131817 if np .prod (values .shape ):
1814- flat = values .ravel ()
1815-
1816- # try with just the first element; we just need to see if
1817- # this is a datetime or not
1818- inferred_type = lib .infer_dtype (flat [0 :1 ])
1819- if inferred_type in ['datetime' , 'datetime64' ]:
1820-
1821- # we have an object array that has been inferred as
1822- # datetime, so convert it
1823- try :
1824- values = tslib .array_to_datetime (
1825- flat ).reshape (values .shape )
1826- if issubclass (values .dtype .type , np .datetime64 ):
1827- klass = DatetimeBlock
1828- except : # it already object, so leave it
1829- pass
1818+ result = _possibly_infer_to_datetimelike (values )
1819+ vtype = result .dtype .type
1820+ if issubclass (vtype , np .datetime64 ):
1821+ klass = DatetimeBlock
1822+ values = result
1823+ elif (issubclass (vtype , np .timedelta64 )):
1824+ klass = TimeDeltaBlock
1825+ values = result
18301826
18311827 if klass is None :
18321828 klass = ObjectBlock
@@ -2525,7 +2521,7 @@ def _consolidate_inplace(self):
25252521 self ._known_consolidated = True
25262522 self ._rebuild_blknos_and_blklocs ()
25272523
2528- def get (self , item ):
2524+ def get (self , item , fastpath = True ):
25292525 """
25302526 Return values for selected item (ndarray or BlockManager).
25312527 """
@@ -2543,7 +2539,7 @@ def get(self, item):
25432539 else :
25442540 raise ValueError ("cannot label index with a null key" )
25452541
2546- return self .iget (loc )
2542+ return self .iget (loc , fastpath = fastpath )
25472543 else :
25482544
25492545 if isnull (item ):
@@ -2553,8 +2549,25 @@ def get(self, item):
25532549 return self .reindex_indexer (new_axis = self .items [indexer ],
25542550 indexer = indexer , axis = 0 , allow_dups = True )
25552551
2556- def iget (self , i ):
2557- return self .blocks [self ._blknos [i ]].iget (self ._blklocs [i ])
2552+ def iget (self , i , fastpath = True ):
2553+ """
2554+ Return the data as a SingleBlockManager if fastpath=True and possible
2555+
2556+ Otherwise return as a ndarray
2557+
2558+ """
2559+
2560+ block = self .blocks [self ._blknos [i ]]
2561+ values = block .iget (self ._blklocs [i ])
2562+ if not fastpath or block .is_sparse or values .ndim != 1 :
2563+ return values
2564+
2565+ # fastpath shortcut for select a single-dim from a 2-dim BM
2566+ return SingleBlockManager ([ block .make_block_same_class (values ,
2567+ placement = slice (0 , len (values )),
2568+ fastpath = True ) ],
2569+ self .axes [1 ])
2570+
25582571
25592572 def get_scalar (self , tup ):
25602573 """
0 commit comments