1
- import collections
1
+ import collections . abc
2
2
import ctypes
3
3
4
4
from typing import Tuple , Any
@@ -14,9 +14,8 @@ def from_dataframe(df : DataFrameXchg,
14
14
"""
15
15
Construct a pandas DataFrame from ``df`` if it supports ``__dataframe__``
16
16
"""
17
- # NOTE: commented out for roundtrip testing
18
- # if isinstance(df, pd.DataFrame):
19
- # return df
17
+ if isinstance (df , pd .DataFrame ):
18
+ return df
20
19
21
20
if not hasattr (df , '__dataframe__' ):
22
21
raise ValueError ("`df` does not support __dataframe__" )
@@ -606,20 +605,31 @@ def get_columns(self):
606
605
for name in self ._df .columns ]
607
606
608
607
def select_columns (self , indices ):
609
- if not isinstance (indices , collections .Sequence ):
608
+ if not isinstance (indices , collections .abc . Sequence ):
610
609
raise ValueError ("`indices` is not a sequence" )
610
+ if not isinstance (indices , list ):
611
+ indices = list (indices )
611
612
612
- return _PandasDataFrameXchg (self ._df .iloc [:, indices ])
613
+ return _PandasDataFrameXchg (self ._df .iloc [:, indices ], self . _nan_as_null , self . _allow_copy )
613
614
614
615
def select_columns_by_name (self , names ):
615
- if not isinstance (names , collections .Sequence ):
616
+ if not isinstance (names , collections .abc . Sequence ):
616
617
raise ValueError ("`names` is not a sequence" )
618
+ if not isinstance (names , list ):
619
+ names = list (names )
617
620
618
- return _PandasDataFrameXchg (self ._df .xs ( names , axis = 'columns' ) )
621
+ return _PandasDataFrameXchg (self ._df .loc [:, names ], self . _nan_as_null , self . _allow_copy )
619
622
620
623
def get_chunks (self , n_chunks = None ):
621
624
"""
622
625
Return an iterator yielding the chunks.
623
626
"""
624
- #TODO: implement chunking when n_chunks > 1
625
- return (self ,)
627
+ if n_chunks and n_chunks > 1 :
628
+ size = len (self ._df )
629
+ step = size // n_chunks
630
+ if size % n_chunks != 0 :
631
+ step += 1
632
+ for start in range (0 , step * n_chunks , step ):
633
+ yield _PandasDataFrameXchg (self ._df .iloc [start :start + step , :], self ._nan_as_null , self ._allow_copy )
634
+ else :
635
+ yield self
0 commit comments