Convert a block of bytes to a Pandas DataFrame Parameters ---------- reader : callable ``pd.read_csv`` or ``pd.read_table``. b : bytestring The content to be parsed with ``reader`` header : bytestring An optional header to prepend to ``b`` kwargs : di
(
reader,
b,
header,
kwargs,
dtypes=None,
columns=None,
write_header=True,
enforce=False,
path=None,
)
| 38 | |
| 39 | |
| 40 | def pandas_read_text( |
| 41 | reader, |
| 42 | b, |
| 43 | header, |
| 44 | kwargs, |
| 45 | dtypes=None, |
| 46 | columns=None, |
| 47 | write_header=True, |
| 48 | enforce=False, |
| 49 | path=None, |
| 50 | ): |
| 51 | """Convert a block of bytes to a Pandas DataFrame |
| 52 | |
| 53 | Parameters |
| 54 | ---------- |
| 55 | reader : callable |
| 56 | ``pd.read_csv`` or ``pd.read_table``. |
| 57 | b : bytestring |
| 58 | The content to be parsed with ``reader`` |
| 59 | header : bytestring |
| 60 | An optional header to prepend to ``b`` |
| 61 | kwargs : dict |
| 62 | A dictionary of keyword arguments to be passed to ``reader`` |
| 63 | dtypes : dict |
| 64 | dtypes to assign to columns |
| 65 | path : tuple |
| 66 | A tuple containing path column name, path to file, and an ordered list of paths. |
| 67 | |
| 68 | See Also |
| 69 | -------- |
| 70 | dask.dataframe.csv.read_pandas_from_bytes |
| 71 | """ |
| 72 | bio = BytesIO() |
| 73 | if write_header and not b.startswith(header.rstrip()): |
| 74 | bio.write(header) |
| 75 | bio.write(b) |
| 76 | bio.seek(0) |
| 77 | df = reader(bio, **kwargs) |
| 78 | if dtypes: |
| 79 | coerce_dtypes(df, dtypes) |
| 80 | |
| 81 | if enforce and columns and (list(df.columns) != list(columns)): |
| 82 | raise ValueError("Columns do not match", df.columns, columns) |
| 83 | if path: |
| 84 | colname, path, paths = path |
| 85 | code = paths.index(path) |
| 86 | df = df.assign( |
| 87 | **{colname: pd.Categorical.from_codes(np.full(len(df), code), paths)} |
| 88 | ) |
| 89 | return df |
| 90 | |
| 91 | |
| 92 | def coerce_dtypes(df, dtypes): |