### Install fastparquet using Pip
Source: https://github.com/dask/fastparquet/blob/main/docs/source/install.md
Install fastparquet from the Python Package Index (PyPI). Ensure numpy is installed first if using pip.
```default
pip install fastparquet
```
--------------------------------
### Partitioned Directory Structure Example
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Provides an example of a Parquet dataset partitioned by 'gender' and 'country' columns, showing the resulting directory tree structure.
```plaintext
table/
: gender=male/
: country=US/
: data.parquet
country=CN/
: data.parquet
gender=female/
: country=US/
: data.parquet
country=CN/
: data.parquet
```
--------------------------------
### Install fastparquet using Conda
Source: https://github.com/dask/fastparquet/blob/main/docs/source/install.md
Use this command to install fastparquet and its dependencies via the conda-forge channel.
```default
conda install -c conda-forge fastparquet
```
--------------------------------
### Install latest fastparquet from GitHub
Source: https://github.com/dask/fastparquet/blob/main/docs/source/install.md
Install the most recent version of fastparquet directly from the main branch on GitHub using pip.
```default
pip install git+https://github.com/dask/fastparquet
```
--------------------------------
### Reading Nested Schema Example
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Illustrates how fastparquet flattens nested Parquet schemas into top-level columns. Nested struct fields like 'visitor.ip' become accessible as regular Pandas columns.
```python
root
| - visitor: OPTIONAL
| - ip: BYTE_ARRAY, UTF8, OPTIONAL
- network_id: BYTE_ARRAY, UTF8, OPTIONAL
```
```python
| - tags: LIST, OPTIONAL
- list: REPEATED
- element: BYTE_ARRAY, UTF8, OPTIONAL
```
--------------------------------
### Get First Row-Group
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Retrieve only the first row-group from a ParquetFile using `next(iter(pf.iter_row_groups()))`.
```python
first = next(iter(pf.iter_row_groups()))
```
--------------------------------
### Build fastparquet documentation
Source: https://github.com/dask/fastparquet/blob/main/docs/source/install.md
Navigate to the docs directory and run 'make html' to build the documentation locally. The output will be in the build/html/ subdirectory.
```bash
# in directory docs/
make html
```
--------------------------------
### Write Parquet to S3 with fastparquet
Source: https://github.com/dask/fastparquet/blob/main/docs/source/filesystems.md
Write parquet data to an S3 file system using a provided open function. The `mkdirs` argument is a no-op for S3 as no intermediate directories need creation.
```python
write('/mybucket/output_parq', data, file_scheme='hive',
row_group_offsets=[0, 500], open_with=myopen, mkdirs=noop)
```
--------------------------------
### Read Parquet from S3 with fastparquet
Source: https://github.com/dask/fastparquet/blob/main/docs/source/filesystems.md
Use s3fs to connect to AWS S3 and read parquet files. Credentials are automatically inferred. The `open_with` argument takes a callable that produces an open file context.
```python
import s3fs
from fastparquet import ParquetFile
s3 = s3fs.S3FileSystem()
myopen = s3.open
pf = ParquetFile('/mybucket/data.parquet', open_with=myopen)
df = pf.to_pandas()
```
--------------------------------
### Environment Variable for JSON Codec
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Demonstrates how to enforce the use of a specific JSON library for object encoding by setting the FASTPARQUET_JSON_CODEC environment variable.
```shell
export FASTPARQUET_JSON_CODEC=orjson
```
--------------------------------
### Read Parquet File with fastparquet
Source: https://github.com/dask/fastparquet/blob/main/README.rst
Use ParquetFile to load data from a parquet file. You can specify columns to load and which to keep as categoricals. Supports single files, metadata files, or directories.
```python
from fastparquet import ParquetFile
pf = ParquetFile('myfile.parq')
df = pf.to_pandas()
df2 = pf.to_pandas(['col1', 'col2'], categories=['col1'])
```
--------------------------------
### Write Parquet with Advanced Options
Source: https://github.com/dask/fastparquet/blob/main/docs/source/quickstart.md
Writes a Pandas DataFrame to a Parquet file with specified row-group sizes, compression, and file scheme (e.g., hive partitioning).
```python
write('outdir.parq', df, row_group_offsets=[0, 10000, 20000],
compression='GZIP', file_scheme='hive')
```
--------------------------------
### Object Encoding with JSON
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Explains how object columns are handled using JSON encoding. Fastparquet utilizes libraries like orjson, ujson, or rapidjson if available for improved performance.
```python
object_encoding = "json"
```
--------------------------------
### Read with Row-Group Filtering
Source: https://github.com/dask/fastparquet/blob/main/docs/source/quickstart.md
Loads data from a Parquet file while skipping row-groups that do not meet specified filter criteria. The filtering column does not need to be loaded.
```python
df3 = pf.to_pandas(['col1', 'col2'], filters=[('col3', 'in', [1, 2, 3, 4])])
```
--------------------------------
### Read Specific Columns and Categories
Source: https://github.com/dask/fastparquet/blob/main/docs/source/quickstart.md
Loads only specified columns from a Parquet file, optionally keeping certain columns as categoricals. Useful for reducing memory usage and load times.
```python
df2 = pf.to_pandas(['col1', 'col2'], categories=['col1'])
```
```python
df2 = pf.to_pandas(['col1', 'col2'], categories={'col1': 12})
```
--------------------------------
### Write Pandas DataFrame to Parquet File
Source: https://github.com/dask/fastparquet/blob/main/docs/source/quickstart.md
Creates a single Parquet file from a Pandas DataFrame. Defaults to plain encoding and no compression.
```python
from fastparquet import write
write('outfile.parq', df)
```
--------------------------------
### Write Parquet File with fastparquet
Source: https://github.com/dask/fastparquet/blob/main/README.rst
Use the 'write' function to save a pandas DataFrame to a parquet file. Supports specifying row group offsets, compression, and file scheme. Defaults to a single file with no compression.
```python
from fastparquet import write
write('outfile.parq', df)
write('outfile2.parq', df, row_group_offsets=[0, 10000, 20000],
compression='GZIP', file_scheme='hive')
```
--------------------------------
### Read Parquet File into Pandas DataFrame
Source: https://github.com/dask/fastparquet/blob/main/docs/source/quickstart.md
Opens a Parquet file and loads its contents into a Pandas DataFrame. Handles multi-file collections transparently.
```python
from fastparquet import ParquetFile
pf = ParquetFile('myfile.parq')
df = pf.to_pandas()
```
--------------------------------
### Write DataFrame with Fixed-Length Text
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Use the 'fixed_text' keyword argument when writing to automatically convert string values to fixed-length byte arrays. This is recommended for binary data but not for general strings due to UTF8 encoding overhead.
```python
write('out.parq', df, fixed_text={'char_code': 1})
```
--------------------------------
### Load Column as Categorical with fastparquet
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
When loading data from other parquet frameworks, specify columns to be loaded as categorical using the 'categories' keyword. This assumes the column is dictionary encoded with consistent labels across the dataset.
```python
pf = ParquetFile('input.parq')
df = pf.to_pandas(categories={'cat': 12})
```
--------------------------------
### Iterate Through Row-Groups
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Use `iter_row_groups` to process datasets larger than memory, loading one row-group at a time. Options similar to `to_pandas` are available for column selection and type conversion.
```python
pf = ParquetFile('myfile.parq')
for df in pf.iter_row_groups():
print(df.shape)
# process sub-data-frame df
```
--------------------------------
### Convert Object Column to Category Type
Source: https://github.com/dask/fastparquet/blob/main/docs/source/details.md
Convert object columns to pandas 'category' type to leverage dictionary encoding for potential performance gains, especially with long labels and low cardinality.
```python
df[col] = df[col].astype('category')
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.