Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" feather-format compat """ 

2 

3from pandas.compat._optional import import_optional_dependency 

4 

5from pandas import DataFrame, Int64Index, RangeIndex 

6 

7from pandas.io.common import stringify_path 

8 

9 

10def to_feather(df: DataFrame, path): 

11 """ 

12 Write a DataFrame to the feather-format 

13 

14 Parameters 

15 ---------- 

16 df : DataFrame 

17 path : string file path, or file-like object 

18 

19 """ 

20 import_optional_dependency("pyarrow") 

21 from pyarrow import feather 

22 

23 path = stringify_path(path) 

24 

25 if not isinstance(df, DataFrame): 

26 raise ValueError("feather only support IO with DataFrames") 

27 

28 valid_types = {"string", "unicode"} 

29 

30 # validate index 

31 # -------------- 

32 

33 # validate that we have only a default index 

34 # raise on anything else as we don't serialize the index 

35 

36 if not isinstance(df.index, Int64Index): 

37 typ = type(df.index) 

38 raise ValueError( 

39 f"feather does not support serializing {typ} " 

40 "for the index; you can .reset_index() " 

41 "to make the index into column(s)" 

42 ) 

43 

44 if not df.index.equals(RangeIndex.from_range(range(len(df)))): 

45 raise ValueError( 

46 "feather does not support serializing a " 

47 "non-default index for the index; you " 

48 "can .reset_index() to make the index " 

49 "into column(s)" 

50 ) 

51 

52 if df.index.name is not None: 

53 raise ValueError( 

54 "feather does not serialize index meta-data on a default index" 

55 ) 

56 

57 # validate columns 

58 # ---------------- 

59 

60 # must have value column names (strings only) 

61 if df.columns.inferred_type not in valid_types: 

62 raise ValueError("feather must have string column names") 

63 

64 feather.write_feather(df, path) 

65 

66 

67def read_feather(path, columns=None, use_threads: bool = True): 

68 """ 

69 Load a feather-format object from the file path. 

70 

71 Parameters 

72 ---------- 

73 path : str, path object or file-like object 

74 Any valid string path is acceptable. The string could be a URL. Valid 

75 URL schemes include http, ftp, s3, and file. For file URLs, a host is 

76 expected. A local file could be: 

77 ``file://localhost/path/to/table.feather``. 

78 

79 If you want to pass in a path object, pandas accepts any 

80 ``os.PathLike``. 

81 

82 By file-like object, we refer to objects with a ``read()`` method, 

83 such as a file handler (e.g. via builtin ``open`` function) 

84 or ``StringIO``. 

85 columns : sequence, default None 

86 If not provided, all columns are read. 

87 

88 .. versionadded:: 0.24.0 

89 use_threads : bool, default True 

90 Whether to parallelize reading using multiple threads. 

91 

92 .. versionadded:: 0.24.0 

93 

94 Returns 

95 ------- 

96 type of object stored in file 

97 """ 

98 import_optional_dependency("pyarrow") 

99 from pyarrow import feather 

100 

101 path = stringify_path(path) 

102 

103 return feather.read_feather(path, columns=columns, use_threads=bool(use_threads))