Metadata-Version: 2.4
Name: polarscope
Version: 1.10.0
Summary: 🔬 Simple data inspection tools for Polars - basic X-ray analysis and utilities for DataFrames.
Author: Anders & Co.
License-Expression: MIT
Project-URL: Homepage, https://github.com/pytoned/polarscope
Project-URL: Repository, https://github.com/pytoned/polarscope
Project-URL: Issues, https://github.com/pytoned/polarscope/issues
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: polars>=1.34
Requires-Dist: plotly>=5.20
Requires-Dist: great_tables>=0.10
Provides-Extra: plotly
Requires-Dist: kaleido>=0.2; extra == "plotly"
Provides-Extra: altair
Requires-Dist: altair>=5.4; extra == "altair"
Provides-Extra: scipy
Requires-Dist: scipy>=1.10; extra == "scipy"
Provides-Extra: all
Requires-Dist: kaleido>=0.2; extra == "all"
Requires-Dist: altair>=5.4; extra == "all"
Requires-Dist: scipy>=1.10; extra == "all"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: psutil>=5.9; extra == "dev"
Requires-Dist: numpy>=1.21; extra == "dev"
Requires-Dist: hypothesis>=6.100; extra == "dev"
Dynamic: license-file

![Polarscope — x-ray your Polars DataFrames: summary stats, distribution nanoplots, outliers and correlations in one table](docs/img/polarscope-cover.svg)

# 🔬 Polarscope - describe() on steroids

**One call, one table: a full profile of any Polars DataFrame. No pandas, anywhere, ever.**

```python
import polars as pl
import polarscope as ps

df = pl.read_csv('your-data.csv')
ps.xray(df)
```

![polarscope xray() profiling 70,000 rows in 21 ms](docs/img/xray-hero.png)

<sub>70,000 rows × 13 columns profiled in 21 ms — every column's dtype, nulls, quartiles, outliers, skew and distribution in a single call.</sub>

[![PyPI](https://img.shields.io/pypi/v/polarscope.svg)](https://pypi.org/project/polarscope/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/polarscope.svg)](https://pypi.org/project/polarscope/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![PyPI Downloads](https://static.pepy.tech/personalized-badge/polarscope?period=total&units=INTERNATIONAL_SYSTEM&left_color=BLACK&right_color=GREEN&left_text=downloads)](https://pepy.tech/projects/polarscope)

**Simple data inspection tools for Polars** 🐻‍❄️

Polarscope is a basic data analysis library for Polars DataFrames. It provides an `xray()` function for data inspection and some plotting utilities. Still early in development with more features planned.

---

## ✨ Current Features

### 🔬 Dataframe Summary Statistics (`xray`)
- Summary statistics for numeric columns by default; `include='all'` covers every dtype.
- Statistics for String/Categorical/Enum columns: top value and frequency, min/median/avg/max length, mode share, top-3 values, and value samples.
- Boolean columns analyzed as 0/1 (Mean = share of True); temporal columns report earliest/latest timestamps.
- Distribution nanoplot per column.
- Optional title for summary table (`title=Your Title`).
- Optional correlation (`corr_target=column_name`) against target for every column with nanoplot visualization.
- Optional expanded output (`expanded=True`) with normality/uniformity tests, outlier metrics and modelling usability flags.
- Optional compact mode (`compact=True`) that shortens big numbers to K (thousands) and M (millions) and decimal precision (`decimals=int`)
- Great Tables output by default or plain Polars DataFrame output (`great_tables=False`).
- Safe on large data: a built-in memory guard checks the memory free on the machine (or container) before the full summary and streams instead when it would not fit - it cannot be switched off.
- Identifier columns (`ORDER_ID`, `customerId`, `kundenummer`, ...) are recognized by name and only counted, not summarized.
- Works on LazyFrames without collecting them: `ps.xray(pl.scan_parquet("big.parquet"))` summarizes by streaming, with memory that stays flat however large the data is; `exact=True` gives the full summary, read one column at a time.

Quickly compute the correlation (Spearman or Pearson) to a target column:

![xray() on a DataFrame with correlation to target column enabled](docs/img/xray_corr.png)

### 🧹 Cleaning & Optimization (`fix`)
- `ps.fix(df)` cleans and optimizes in one call: column renaming (`case="snake"/"camel"/"pascal"/"kebab"/"upper"/"lower"`), whitespace stripping (empty strings → null), dtype shrinking, and empty-column removal — with a compact report of what changed.
- Opt-in extras: `drop_duplicate_rows`, `missing_threshold`, `drop_constant_columns`, `outliers="iqr"/"zscore"`.
- Lossless by default: anything that removes or alters data must be switched on explicitly. Dtype shrinking never changes a value: Float64 becomes Float32 only when every value survives exactly, and integers shrink no further than Int32 (`min_int_bits=32`), because Polars integer arithmetic wraps on overflow and narrower types overflow sooner in later calculations.
- Building blocks also available standalone: `clean_column_names`, `convert_datatypes`, `drop_missing`.

### 📈 Plots
- Correlation/missing/distribution/categorical plots (plotly or altair backends)

### 📊 Built-in Datasets
- Three datasets ship with the package: `ps.titanic()`, `ps.diabetes()`, and `ps.cardio()`.
- Also available via `from polarscope.datasets import titanic, diabetes, cardio`, with loader functions.

### ✅ Polars-Only Data Handling
- Data handling is implemented with Polars.
- No Pandas, anywhere, ever. 

---

### 🔗 Customizing the Polarscope output

Polarscope is build on top of amazing libraries like Great Tables, Plotly and Altair. Polarscope functions like ps.xray() return native objects from these libraries 
(such as a GT table, a Plotly Figure, or an Altair Chart), so you retain complete access to their underlying APIs. This allows you to seamlessly chain native methods to further customize styling, annotations, 
and layout configurations directly on the Polarscope output. Below are three examples demonstrating how to extend these objects.

Great Tables customization:

```python
from great_tables import md, loc, style   # imported to enable extra functionality

(
    ps.xray(df[:, :6])
    .tab_header(
        title = "Example: GT method chaining onto ps.xray() with custom table styling, a source note and a footnote in markdown.",
        subtitle = ""
    )
    .tab_footnote(footnote=md("Some ***very*** insightful footnote about the data."))
    .tab_source_note("Source: data gathered from the Polarscope Times")
    .tab_style(
        style=style.text(color="purple", weight="bold"),
        locations=loc.footer()
    )
    .tab_options(
        table_width='60%',
        table_background_color="#F1E1FC",
        heading_background_color="#F1E1FC",
        column_labels_background_color="#F1E1FC",
        table_font_names="DejaVu Sans",
        column_labels_font_weight="bold"
    )
)
```

Customizing a GT table using method chaining:

![Customizing a GT table using method chaining](docs/img/gt_styling.png)


Plotly customization:

```python
(
    # base plot using Plotly backend
    ps.cat_plot(df.select(pl.col('sex', 'embarked')))
    
    # Customize the Plotly go object
    .update_traces(
        cliponaxis=False,
        marker_color="#25023b"  # Changes the fill color of the bars
    )
    .update_layout(
        template="plotly_white", 
        title="Example of Plotly customization",
        margin={"t": 100, "b": 100},
        plot_bgcolor="#F1E1FC",
        paper_bgcolor="#F1E1FC",
    )
)
```

Customizing a Plotly go object using method chaining:

![Customizing a Plotly chart](docs/img/plotly_styling.png)


Altair customization:

```python
import altair as alt

# base correlation plot using Polarscope corr_plot() with Altair backend
base_chart = ps.corr_plot(df[:, :9], width=600, height=300, backend="altair")

# Fetch the correlation values directly from the base chart
text_labels = base_chart.mark_text(baseline='middle').encode(
    text=alt.Text('correlation:Q', format='.0%'),
    
    # Dynamically change text color based on the correlation value and use white text if cell background is dark
    color=alt.condition(
        abs(alt.datum.correlation) > 0.7,
        alt.value('white'),
        alt.value('black')
    )
)

# Layer the charts (+) and chain final configurations onto the result
final_chart = (
    (base_chart + text_labels)
    .properties(
        title="Example of Altair backend chart customization",
        padding={"left": 40, "right": 40, "top": 20, "bottom": 20}
    )
    .configure_title(
        font="DejaVu Sans",
        color="darkblue",
        anchor="start",
        fontSize=20,
    )
)
final_chart.show()
```

Customizing an Altair chart object using method chaining:

![Customizing an Altair chart object using method chaining](docs/img/altair_styling.png)

---

## 🔍 How Polarscope compares

| | **polarscope** 1.10.0 | skimpy 0.0.21 | ydata-profiling 4.18.4 | klib 1.4.1 |
|---|---|---|---|---|
| Takes a Polars DataFrame directly | ✅ | ✅ | ❌ convert to pandas first | ❌ convert to pandas first |
| Requires/converts to pandas | **✅ never** | ❌ hard dependency | ❌ | ❌ |
| Core dependencies | **3** | 12 | 21 | 8 |
| Minimum Python | **3.9** | 3.11 | 3.10 | 3.10 |
| Primary output | Great Tables HTML, or Polars DataFrame | terminal (rich) | standalone HTML report | matplotlib/seaborn figures |
| String / categorical stats | ✅ | ✅ | ✅ | partial |
| One-call cleaning and dtype optimization | ✅ `ps.fix()` | ❌ | ❌ | ✅ |

**Where the others are the better choice:**

- **ydata-profiling** produces a far deeper report in one call — variable interactions, correlation matrices, alerts and warnings. If you want an exhaustive standalone HTML artifact and don't mind the 21 dependencies or the pandas conversion, it does more than polarscope does.
- **skimpy** also reads Polars natively and prints straight to the terminal, which is the nicer fit for CLI and script workflows. Polarscope targets notebooks with presentation ready outputs.
- **klib** has a mature pandas cleaning and plotting suite. Polarscope's plotting API is openly modelled on it.

**Where polarscope wins:** it is the only one of the four with **no pandas anywhere in its dependency tree**, and the only one installable on Python 3.9 — which matters if your environment is locked down and you cannot pull in pandas, seaborn and numba just to look at a table.

<sub>Verified 2026-08-10 against each project's published PyPI metadata (`requires_dist`, `requires_python`) at the versions listed. Dependency counts exclude optional extras. Corrections welcome — please open an issue.</sub>

---

## 🚀 Quick Start

### Installation

```bash
pip install polarscope
```

Requires Python 3.9+ and Polars 1.34+ (the first release that streams query
results back in batches, which keeps `xray()`'s memory bounded on large
LazyFrames).

Optional extras:

```bash
pip install "polarscope[altair]"   # optional Altair plotting backend (Altair 5.4+, no pandas)
pip install "polarscope[plotly]"   # Kaleido support for static Plotly images
pip install "polarscope[scipy]"    # statistical tests
pip install "polarscope[all]"      # all optional features
pip install "polarscope[dev]"      # pytest + coverage tools
```

Plotly is included in the base installation and is the default plotting backend.

### Basic Usage

```python
import polars as pl
import polarscope as ps

# Use a built-in dataset or load your own
df = ps.titanic()          # also: ps.diabetes(), ps.cardio()
# df = pl.read_csv("your_data.csv")

# Get basic data summary (all column types)
ps.xray(df, include="all")

# More detailed analysis
ps.xray(df, include="all", expanded=True)

# Custom title and correlation analysis
ps.xray(df, title="My Data Analysis", corr_target="Survived")

# Clean & optimize in one call (snake_case names, trimmed strings,
# shrunk dtypes, empty columns dropped - with a report)
df = ps.fix(df)

# Or opt in to deeper cleaning
df = ps.fix(df, case="camel", drop_duplicate_rows=True, missing_threshold=0.9)
```

### Common `xray()` Options

```python
ps.xray(
    df,
    include="all",                # include all columns (not only numeric)
    expanded=True,                # additional stats/tests/quality fields
    corr_target="column_name",    # correlation against target column
    outlier_method="iqr",         # or "percentile"/"zscore"
    percentiles=[0.1, 0.5, 0.9],  # custom percentiles
    decimals=2,
    great_tables=False            # return Polars DataFrame
)
```

### Large Data: X-ray a LazyFrame

`xray()` accepts a `LazyFrame` and never collects it. By default a LazyFrame
is summarized by **streaming**: one pass over the analyzed columns plus a small
group-by per column, so no column is ever held in memory and memory stays flat
however many rows there are - the way to summarize a file larger than memory.

```python
lf = pl.scan_parquet("big.parquet")   # or scan_ipc, or any lazy query

ps.xray(lf)                     # streaming statistics, numeric columns only
ps.xray(lf, include="all", expanded=True, corr_target="price")
ps.xray(lf, exact=True)         # the full DataFrame summary, one column at a time
```

The streaming summary shows count, nulls/NaN, % missing, mean, std, min, max,
skew, kurtosis, zero/positive/negative shares, a histogram, outliers, Pearson
correlation, string lengths and earliest/latest; expanded mode adds the distinct
count (exact up to a million distinct values, estimated within about 0.1%
above), `opt_dtype`, quality flags and the statistical tests (on a systematic
sample of about 3,000 rows). String columns with up to 100,000 distinct values
also get their top values. Every value shown equals the one a DataFrame gets.
What it leaves out needs all of a column's values at once:

| Not in the streaming summary | Instead |
|---|---|
| 25% / 50% / 75%, IQR, MAD | `exact=True` |
| IQR outliers | outliers use the z-score rule (beyond mean ± 3 std) |
| duplicate counts | the distinct count |
| Spearman correlation, custom `percentiles` | `exact=True` |

With `exact=True` a LazyFrame gets results identical to a DataFrame. It is read
in batches of at most 10 million cells (rows × columns), one column at a time
when a column alone is larger; each batch is summarized and released before
the next is read. The frame need not fit in memory, but sorting a column copies
it, so this needs several times the largest column's size.

Measured with `include="all"` on a 12-column Parquet dataset (integers, two
Decimal price columns, floats with NaN, a Boolean, two strings, a date and a
timestamp), on a 4-core machine with 15 GB of RAM, each run in a fresh process:

| Rows | `xray(pl.read_parquet(...))` | `xray(lf, exact=True)` | `xray(lf)` (streaming) | streaming, `expanded=True` |
|---|---|---|---|---|
| 20 million | 9.3 s, 3.45 GB | 9.2 s, 1.48 GB | **5.0 s, 0.37 GB** | 9.5 s, 0.49 GB |
| 100 million | - | - | **18.6 s, 0.74 GB** | 37.9 s, 1.00 GB |
| 200 million (about 19 GB in memory) | stopped at 12 GB | stopped at 12 GB | **35.5 s, 0.89 GB** | 72.0 s, 1.25 GB |

Streaming memory stays around 1 GB from 100 to 200 million rows; most of it is
Polars' Parquet read-ahead (several files at once).

### Memory guard

`xray()` never computes a summary that would not fit. Before a full summary (a
DataFrame, or a LazyFrame with `exact=True`) it estimates the memory needed from
the columns' sizes and compares it with the memory free right now: the
operating system's available memory, capped by any container (cgroup) limit.
If the summary needs more than half of it, `xray()` summarizes by streaming
instead and says so in a warning and under the table. If the call explicitly
asked for something only the full summary gives (IQR or percentile outliers,
custom `percentiles`, Spearman correlation), it raises `MemoryError` with the
numbers instead. There is no option to switch the guard off.

Either way, every query re-evaluates the lazy plan. That is cheap for
`scan_parquet` and `scan_ipc`, which read only the columns a query needs, but
slow for `scan_csv` (every query parses the whole file) or a plan with
expensive steps such as joins, and a plan that samples or shuffles rows without
a fixed seed gives each query different rows. Collect those first, or write
them to Parquet with `sink_parquet()` and scan that. The subtitle reads
"LazyFrame" where a DataFrame shows its in-memory size, since a LazyFrame has
no size until collected.

### Customizing Beyond the Built-in Options

polarscope returns the underlying library's own object rather than a wrapper, so
anything `great_tables` or your plotting backend can do is available by chaining
onto the result — no polarscope parameter needed.

`ps.xray()` returns a `GT` object. Chained calls run after polarscope's own
styling, so they win where the two overlap:

```python
from great_tables import loc, md

(
    ps.xray(df)
    .tab_options(table_background_color="#fdf6e3")
    .tab_footnote(
        footnote=md("Excludes nulls."),
        locations=loc.body(columns="mean", rows=[0]),
    )
)
```

Column names are not relabelled, so the names from `great_tables=False` output
are the identifiers to use in chained calls.

The plotting functions return a Plotly `Figure` or Altair `Chart`:

```python
# Per figure
ps.missingval_plot(df).update_layout(template="plotly_dark")

# Or globally, using the backend's own theme setting
import plotly.io as pio
pio.templates.default = "plotly_dark"
```

---

## 🤝 Contributing

Contributions are welcome! Feel free to report bugs or suggest improvements.

---

## 📄 License

MIT License - see [LICENSE](LICENSE) file for details.

---

## 🙏 Acknowledgments

Inspired by [`klib`](https://github.com/akanz1/klib) and built with [`Polars`](https://github.com/pola-rs/polars) and [`great_tables`](https://github.com/posit-dev/great-tables).

---

**🔬 A quick and lightweight tool for Polars dataframe stats and visualizations.**
