File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -343,6 +343,37 @@ def __getitem__(self, key):
343
343
else : # pragma: no cover
344
344
raise
345
345
346
+ def icol (self , i ):
347
+ """
348
+ Retrieve the i-th column or columns of the DataFrame by location
349
+
350
+ Parameters
351
+ ----------
352
+ i : int, slice, or sequence of integers
353
+
354
+ Notes
355
+ -----
356
+ If slice passed, the resulting data will be a view
357
+
358
+ Returns
359
+ -------
360
+ column : Series (int) or DataFrame (slice, sequence)
361
+ """
362
+ if isinstance (i , slice ):
363
+ # need to return view
364
+ lab_slice = slice (label [0 ], label [- 1 ])
365
+ return self .ix [:, lab_slice ]
366
+ else :
367
+ label = self .columns [i ]
368
+ if isinstance (label , Index ):
369
+ if self .columns .inferred_type == 'integer' :
370
+ # XXX re: #2228
371
+ return self .reindex (columns = label )
372
+ else :
373
+ return self .ix [:, i ]
374
+
375
+ return self [label ]
376
+
346
377
@Appender (DataFrame .get_value .__doc__ , indents = 0 )
347
378
def get_value (self , index , col ):
348
379
s = self ._series [col ]
Original file line number Diff line number Diff line change @@ -907,6 +907,12 @@ def test_icol(self):
907
907
self .assertTrue (isinstance (result , SparseSeries ))
908
908
assert_sp_series_equal (result , self .frame ['A' ])
909
909
910
+ # preserve sparse index type. #2251
911
+ data = {'A' : [0 ,1 ]}
912
+ iframe = SparseDataFrame (data , default_kind = 'integer' )
913
+ self .assertEquals (type (iframe ['A' ].sp_index ),
914
+ type (iframe .icol (0 ).sp_index ))
915
+
910
916
def test_set_value (self ):
911
917
res = self .frame .set_value ('foobar' , 'B' , 1.5 )
912
918
self .assert_ (res is not self .frame )
You can’t perform that action at this time.
0 commit comments