mortie.pandas
mortie's pandas extension — not pandas itself. This module holds the
morton_index pandas ExtensionDtype / ExtensionArray pair over the packed
64-bit decimal-Morton words. Importing it is what pulls pandas in, which is how
import mortie stays numpy-only (see
Optional dependencies).
!!! note "Import paths"
The members below are documented under `mortie.pandas` because that is
where they are defined. The canonical user-facing import is the short one:
```python
from mortie import MortonIndexArray, MortonIndexDtype
```
`from mortie.pandas import MortonIndexArray` and
`from mortie.morton_index import MortonIndexArray` resolve to the very same
class objects — all three paths work, and none is deprecated.
For narrative usage — construction, the decimal repr, sorting, the domain
accessors and the parquet round-trip — see
The morton_index datatype.
mortie's pandas extension: the morton_index ExtensionArray.
This is mortie's pandas integration layer, not pandas itself -- the
ExtensionDtype / ExtensionArray pair over the packed 64-bit decimal-Morton
words. The numpy-only parse surface and the packed-word scalar live next door
in :mod:mortie.morton_index.
pandas stays an optional dependency: importing this module is what pulls
pandas in, and mortie imports it only on demand -- when the ExtensionArray
names are actually requested, or once at import time when pandas is already
installed, so the "morton_index" dtype string is registered. A numpy-only
install therefore imports mortie fine and gets the curated ImportError
from :func:mortie.morton_index._require_pandas if it touches the classes.
The classes sit at module level here -- not inside a function, as they did
before issue #135 -- so they are ordinary, statically discoverable attributes:
vars(), dir(), :func:inspect.getmembers and mkdocstrings all see them,
and __qualname__ is the bare class name. That needs a module free to import
pandas at its own top level, which is what this one is for.
MortonIndexArray
Bases: ExtensionArray
An array of packed 64-bit morton_index MOC words.
Construct from raw words with the constructor, or from a HEALPix NESTED
index via :meth:from_nested / a lat/lon via :meth:from_latlon.
Comparisons and sorting use the raw uint64 (the Z-order); the domain
methods :meth:coarsen, :meth:orders/:meth:order,
:meth:base_cells/:meth:base_cell and :meth:is_fixed_order delegate
to the vectorized Rust bindings. No arithmetic operators are defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
array_like
|
1-D array-like of packed |
required |
copy
|
bool
|
Copy |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in mortie/pandas.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 | |
dtype
property
The morton_index ExtensionDtype of this array.
Returns:
| Type | Description |
|---|---|
MortonIndexDtype
|
The singleton dtype instance. |
nbytes
property
Size of the packed-word storage in bytes.
Returns:
| Type | Description |
|---|---|
int
|
Bytes consumed by the underlying |
__arrow_c_array__(requested_schema=None)
Return Arrow C-Data (schema, array) capsules over the words.
The empty sentinel is exported as an Arrow null (validity bitmap) and
the schema carries the morton_index extension type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requested_schema
|
PyCapsule
|
Accepted (per the protocol) but ignored: this array has a single fixed logical type. |
None
|
Returns:
| Type | Description |
|---|---|
tuple of PyCapsule
|
The |
Source code in mortie/pandas.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
__arrow_c_schema__()
Return the Arrow C-Data schema capsule for morton_index.
Returns:
| Type | Description |
|---|---|
PyCapsule
|
An |
Source code in mortie/pandas.py
327 328 329 330 331 332 333 334 335 336 337 338 | |
__eq__(other)
Elementwise equality (bit-identity of the packed words).
Source code in mortie/pandas.py
628 629 630 631 632 | |
__ge__(other)
Elementwise >= in Z-order (raw uint64 order).
Source code in mortie/pandas.py
658 659 660 661 662 | |
__getitem__(item)
Index or slice the array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
int or slice or array_like
|
A scalar position, or any numpy-style selection. |
required |
Returns:
| Type | Description |
|---|---|
MortonIndexScalar or MortonIndexArray
|
A scalar position yields a :class: |
Source code in mortie/pandas.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
__gt__(other)
Elementwise > in Z-order (raw uint64 order).
Source code in mortie/pandas.py
652 653 654 655 656 | |
__le__(other)
Elementwise <= in Z-order (raw uint64 order).
Source code in mortie/pandas.py
646 647 648 649 650 | |
__len__()
Return the number of words in the array.
Source code in mortie/pandas.py
446 447 448 | |
__lt__(other)
Elementwise < in Z-order (raw uint64 order).
Source code in mortie/pandas.py
640 641 642 643 644 | |
__ne__(other)
Elementwise inequality (bit-identity of the packed words).
Source code in mortie/pandas.py
634 635 636 637 638 | |
__repr__()
Render the array as decimal ids with its length and order.
Source code in mortie/pandas.py
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 | |
__setitem__(key, value)
Assign words in place, mapping NA markers to the sentinel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
int or slice or array_like
|
The positions to assign. |
required |
value
|
MortonIndexArray or scalar or sequence
|
The replacement word(s). |
required |
Source code in mortie/pandas.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
base_cell()
Return the single shared base cell of the array.
Returns:
| Type | Description |
|---|---|
int or None
|
The shared base cell, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the array spans multiple base cells; use :meth: |
Source code in mortie/pandas.py
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | |
base_cells()
Return the per-element HEALPix base cell (0..=11).
Returns:
| Type | Description |
|---|---|
ndarray
|
The base cell of each element; empty / invalid words map to
|
Source code in mortie/pandas.py
697 698 699 700 701 702 703 704 705 706 | |
coarsen(k)
Coarsen every word to order k (a new array; suffix rewrite).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The target HEALPix order. Elements already at or below order
|
required |
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
A new array of coarsened words. |
Source code in mortie/pandas.py
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | |
copy()
Return a deep copy of the array.
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
A new array over a copied word buffer. |
Source code in mortie/pandas.py
519 520 521 522 523 524 525 526 527 | |
decimal_repr()
Decode each word to its decimal Morton string (issue #48).
The human-readable decimal Morton form produced by decoding each
word (the canonical render-only repr; backward-compatible with the
legacy str(legacy_i64) for orders 0..=18, the natural extension
for 19..=29). Point words carry the terminal p kind suffix
(spec section 2, issue #120), so the repr is injective across kinds
and the round-trip is lossless.
Returns:
| Type | Description |
|---|---|
list of str
|
One decimal Morton id per element. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On any empty / invalid word. |
Source code in mortie/pandas.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 | |
from_arrow(source)
classmethod
Build a MortonIndexArray from any Arrow C-Data array (#93).
The words are pulled over the PyCapsule C Data Interface with no
pyarrow dependency; Arrow nulls come back as the all-zero empty
sentinel so :meth:isna round-trips. This is the
library-agnostic sibling of :func:mortie.arrow.to_morton_index
(the pyarrow ExtensionArray path).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
object or tuple
|
An object exposing |
required |
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The imported packed words. |
Source code in mortie/pandas.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
from_decimal(decimals)
classmethod
Parse decimal Morton strings into an array (issue #114).
The inverse of :meth:to_decimal, and sugar over the numpy-only
:func:decimals_to_words for pandas users -- to_decimal()
output round-trips straight back through it. An unmarked order-29
id yields the AREA word; only a p-marked one yields the POINT
word (spec section 4), so point-ness does not survive a round-trip
through an unmarked string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decimals
|
array_like of str
|
Decimal Morton ids, e.g. the output of :meth: |
required |
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The parsed packed words. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Naming the first malformed id. |
Source code in mortie/pandas.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
from_hive_path(paths, suffix='.zarr')
classmethod
Parse hive-layout paths back to words (inverse of hive_path).
The leaf basename carries the full decimal id; when the path also
carries the digit directories -- recognized by a
{sign+base}-shaped component sitting at its slot above the leaf
-- the whole chain is checked against the leaf. A bare
{full_id}.zarr, or one under an arbitrary root without the
digit chain, skips the check. Order-29 ids parse to the area word
(see :func:decimal_to_word).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
str or PathLike or iterable
|
A single slash-separated path or an iterable of them. |
required |
suffix
|
str
|
The leaf extension to strip. Default |
'.zarr'
|
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The parsed packed words, in input order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a leaf does not end with |
Source code in mortie/pandas.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 | |
from_latlon(lat, lon, order=MAX_ORDER, points=False)
classmethod
Hash lat/lon (degrees) to morton_index words at order.
Routes through the Rust healpix bridge: lat/lon -> NESTED ids ->
packed words, so it matches the cross-library nested representation.
With points=False (the default) the result is an order-order
area cell (Kind::Area). With points=True the location is
encoded as a max-resolution point (Kind::Point); point
encoding is order-29-only, so an explicit order != 29 raised
together with points=True is a ValueError (the default
order is MAX_ORDER so the point path needs no extra argument).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat
|
array_like
|
Latitudes in degrees. |
required |
lon
|
array_like
|
Longitudes in degrees. Must have the same shape as |
required |
order
|
int
|
HEALPix order to hash at. Default :data: |
MAX_ORDER
|
points
|
bool
|
Encode a max-resolution point instead of an area cell.
Default |
False
|
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The packed words, one per input coordinate. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in mortie/pandas.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
from_legacy(legacy)
classmethod
Convert retired legacy decimal Morton int64 values to words.
One-way bridge (issue #48): the legacy decimal encoding is being
retired in favour of the packed word, but the converter is kept for
checking new output against old pinned values. There is no packed ->
legacy inverse beyond the render-only :meth:decimal_repr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
legacy
|
array_like
|
Legacy signed decimal Morton values, convertible to |
required |
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The equivalent packed words. |
Source code in mortie/pandas.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
from_nested(nested, depth)
classmethod
Pack HEALPix NESTED ids at depth into morton_index words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nested
|
array_like
|
NESTED cell ids. |
required |
depth
|
int
|
The scalar HEALPix order they were hashed at. |
required |
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The packed words. |
Source code in mortie/pandas.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
from_words(words, copy=False)
classmethod
Wrap an array of already-packed uint64 words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words
|
array_like
|
1-D array-like of packed |
required |
copy
|
bool
|
Copy |
False
|
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The wrapped words. |
Source code in mortie/pandas.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
hive_path(root='', suffix='.zarr')
Build the hive-layout path per element (issue #104; spec lands on #62).
The morton-hive/1 convention (zagg's sparse-coverage design
record): one decimal digit per directory level, the full id as the
leaf inside its own node --
{root}/{sign+base}/{d1}/.../{d_order}/{full_id}{suffix}, e.g.
-31123 -> -3/1/1/2/3/-31123.zarr and the order-0 -3 ->
-3/-3.zarr. Every order is a node, so mixed shard orders nest
naturally: a coarser shard's leaf sits in the same directory its
finer siblings descend through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str
|
Path prefix placed above the digit chain. Default |
''
|
suffix
|
str
|
Extension appended to the leaf. Default |
'.zarr'
|
Returns:
| Type | Description |
|---|---|
list of str
|
One hive path per element. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On any empty / invalid word, or for a point id -- points do not live in paths, and the kind suffix never enters a path component (spec section 2, issue #120). |
Source code in mortie/pandas.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | |
is_fixed_order()
Report whether every element shares one HEALPix order.
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in mortie/pandas.py
731 732 733 734 735 736 737 738 739 740 741 742 743 | |
isna()
Return a boolean mask of the missing elements.
The empty sentinel (all-zero word, prefix 0) is the missing value.
Returns:
| Type | Description |
|---|---|
ndarray
|
A |
Source code in mortie/pandas.py
507 508 509 510 511 512 513 514 515 516 517 | |
order()
Return the single shared order of a fixed-order array.
Returns:
| Type | Description |
|---|---|
int or None
|
The shared HEALPix order, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the array holds mixed orders; use :meth: |
Source code in mortie/pandas.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | |
orders()
Return the per-element HEALPix order.
Returns:
| Type | Description |
|---|---|
ndarray
|
A |
Source code in mortie/pandas.py
666 667 668 669 670 671 672 673 674 | |
take(indices, *, allow_fill=False, fill_value=None)
Take elements by position (pandas protocol).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
array_like of int
|
Positions to take. |
required |
allow_fill
|
bool
|
Treat |
False
|
fill_value
|
scalar
|
The value to fill with when |
None
|
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The taken words. |
Source code in mortie/pandas.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | |
to_decimal()
Emit the decimal strings as a fixed-width numpy array.
The always-strings interchange convention from issue #48. Point
words carry the p suffix (issue #120).
Returns:
| Type | Description |
|---|---|
ndarray
|
The decode-through-kernel decimal strings as a |
Raises:
| Type | Description |
|---|---|
ValueError
|
On any empty / invalid word. |
Source code in mortie/pandas.py
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
to_legacy_i64()
Emit the legacy signed decimal int64 form (orders <= 18).
The named escape hatch from issue #48's emit conventions --
interchange is always the decimal string (:meth:to_decimal),
storage the packed uint64; this exists solely for testing new
output against old pinned values (pairing with
:meth:from_legacy).
Returns:
| Type | Description |
|---|---|
ndarray
|
The legacy signed decimal values as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any element is above order 18 (the legacy decimal overflows
|
Source code in mortie/pandas.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 | |
to_nested()
Decode the words to HEALPix NESTED ids via the kernel.
Returns:
| Type | Description |
|---|---|
tuple of numpy.ndarray
|
The |
Source code in mortie/pandas.py
762 763 764 765 766 767 768 769 770 | |
MortonIndexDtype
Bases: ExtensionDtype
pandas dtype registered as "morton_index".
Backed by uint64 storage (the raw packed Morton words; issue #58).
The missing value is pd.NA, stored as the kernel's all-zero empty
sentinel.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The registered dtype name, |
type |
type
|
The scalar type backing the dtype, |
kind |
str
|
The numpy kind character, |
na_value |
NA
|
The missing value this dtype reports. |
Source code in mortie/pandas.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
__from_arrow__(array)
Build a :class:MortonIndexArray from a pyarrow array.
This is the hook pandas calls on table.to_pandas() for a column
tagged with the morton_index Arrow extension type, so the words
land back as a MortonIndexArray (not a plain int64 Series).
The pyarrow import stays lazy so this module remains numpy-only
importable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
Array or ChunkedArray
|
The Arrow column to convert; a chunked array is concatenated. |
required |
Returns:
| Type | Description |
|---|---|
MortonIndexArray
|
The packed words as a pandas ExtensionArray. |
Source code in mortie/pandas.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
construct_array_type()
classmethod
Return the ExtensionArray class this dtype constructs.
Returns:
| Type | Description |
|---|---|
type
|
:class: |
Source code in mortie/pandas.py
76 77 78 79 80 81 82 83 84 85 | |