Paste data as Python DataFrame definitions
Project description
datapasta
A Python package inspired by the R datapasta package for pasting tabular data into DataFrame code. datapasta analyzes clipboard content or text input and generates Python code to recreate the data as a pandas or polars DataFrame.
Features
- Automatic detection of delimiters (comma, tab, pipe, semicolon, etc.)
- Smart header detection
- Type inference for columns (int, float, boolean, datetime, string)
- Generates code for both pandas and polars DataFrames
- Command-line interface for easy integration with text editors
- Simple API for programmatic use
Installation
# Install with pip
pip install datapasta
# With Pandas support
pip install datapasta[pandas]
# With Polars support
pip install datapasta[polars]
# For Polars on older CPUs
pip install datapasta[polars-lts-cpu]
The
polarsdependency is not included in the package by default. It is shipped as an optional extra which can be activated by passing it in square brackets.
Usage
Command Line
# Read from clipboard, generate pandas code
datapasta > dataframe_code.py
# Read from clipboard, generate polars code
datapasta --polars > dataframe_code.py
# Read from file instead of clipboard
datapasta --file data.csv > dataframe_code.py
# Specify a separator (otherwise auto-detected)
datapasta --sep "," > dataframe_code.py
Python API
import datapasta
# Read from clipboard and get pandas code
pandas_code = datapasta.clipboard_to_pandas()
print(pandas_code)
# Read from clipboard and get polars code
polars_code = datapasta.clipboard_to_polars()
print(polars_code)
# Convert text directly to DataFrame code
csv_text = """name,age,city
Alice,25,New York
Bob,30,San Francisco
Charlie,35,Seattle"""
pandas_code = datapasta.text_to_pandas(csv_text)
print(pandas_code)
Controlling Header Detection
datapasta attempts to automatically detect whether your data has a header row, but you can override this behavior when needed:
Command Line
# Auto-detect headers (default behavior)
datapasta --file data.csv
# Force using the first row as a header
datapasta --file data.csv --header yes
# Force no header (generate column names like col1, col2, etc.)
datapasta --file data.csv --header no
Python API
import datapasta
# Auto-detect headers (default)
code = datapasta.text_to_pandas(text)
# Force using the first row as a header
code = datapasta.text_to_pandas(text, has_header=True)
# Force no header
code = datapasta.text_to_pandas(text, has_header=False)
This is particularly useful when:
- The auto-detection logic misidentifies numeric headers as data
- You want to preserve the first row as data but datapasta treats it as a header
- You need consistent column names (col1, col2, etc.) for multiple similar datasets
Examples
From a CSV in the clipboard
name,age,city
Alice,25,New York
Bob,30,San Francisco
Charlie,35,Seattle
datapasta will generate:
import pandas as pd
df = pd.DataFrame({
"name": ["Alice", "Bob", "Charlie"],
"age": [25, 30, 35],
"city": ["New York", "San Francisco", "Seattle"],
})
From a TSV in the clipboard
name age city
Alice 25 New York
Bob 30 San Francisco
Charlie 35 Seattle
datapasta will generate similar code, automatically detecting the tab delimiter.
Using in a Jupyter notebook
import datapasta
# Assuming you've copied data to clipboard
code = datapasta.clipboard_to_pandas()
print("Generated code:")
print(code)
# Execute the code to create the DataFrame
exec(code)
# Now 'df' contains your DataFrame
display(df)
How It Works
datapasta works by:
- Reading text from the clipboard or a file
- Intelligently guessing the delimiter/separator
- Detecting if there's a header row
- Inferring column types (int, float, boolean, datetime, string)
- Generating code to create a pandas or polars DataFrame
Project Structure
clipboard.py: Functions for reading from the system clipboardparser.py: Functions for parsing text data, detecting delimiters, and headerstype_inference.py: Functions for inferring column data typesformatter.py: Functions for generating pandas and polars codemain.py: Main entry points and CLI functionality
Contributing
Contributions welcome!
- Issues & Discussions: Please open a GitHub issue or discussion for bugs, feature requests, or questions.
- Pull Requests: PRs are welcome!
- Install the dev extra with
pip install -e ".[dev]" - Run tests with
pytest - Include updates to docs or examples if relevant
- Install the dev extra with
Requirements
- Python 3.10+
- pyperclip (for clipboard access)
License
This project is licensed under the MIT License.
Credits
Inspired by the R package datapasta by Miles McBain.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datapasta-0.1.0.tar.gz.
File metadata
- Download URL: datapasta-0.1.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.3 CPython/3.11.11 Linux/6.8.0-51-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60b4c598e5d96097a2ce007ab74c7e7a1cfa49f20fad8003e8e5a0955d6043a1
|
|
| MD5 |
b9aacfa3cbc62b6582f68f648af6d755
|
|
| BLAKE2b-256 |
1d43ddb120a883f711b09582732ad1c57149eaab4d2413737693c8f1444ba649
|
File details
Details for the file datapasta-0.1.0-py3-none-any.whl.
File metadata
- Download URL: datapasta-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.3 CPython/3.11.11 Linux/6.8.0-51-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba79672b6df5fa5a04516bcad484aeb57e32ba1cb2243e553c0d55d84780cfe3
|
|
| MD5 |
2969553c98addb4a6285be8f52cef6d8
|
|
| BLAKE2b-256 |
15f54beae48da72a60583c7c55fb64b324c1e47a79f1130988eb0bb36ad65422
|