Core Python library for astronomy and astrophysics workflows that need Astropy APIs, including units/quantities, coordinates, FITS I/O, tables, time systems, WCS, and cosmology. Use when implementing or debugging astronomical data analysis code with Astropy.
import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.time import Time
from astropy.io import fits
from astropy.table import Table
from astropy.cosmology import Planck18
# Units and quantities
distance = 100 * u.pc
distance_km = distance.to(u.km)
# Coordinates
coord = SkyCoord(ra=10.5*u.degree, dec=41.2*u.degree, frame='icrs')
coord_galactic = coord.galactic
# Time
t = Time('2023-01-15 12:30:00')
jd = t.jd # Julian Date
# FITS files
data = fits.getdata('image.fits')
header = fits.getheader('image.fits')
# Tables
table = Table.read('catalog.fits')
# Cosmology
d_L = Planck18.luminosity_distance(z=1.0)
astropy.units).to() methodreferences/units.md for comprehensive documentation, unit systems, equivalencies, performance optimization, and unit arithmetic.astropy.coordinates)SkyCoord in any frame (ICRS, Galactic, FK5, AltAz, etc.)references/coordinates.md for detailed coordinate frame descriptions, transformations, observer-dependent frames (AltAz), catalog matching, and performance tips.astropy.cosmology)references/cosmology.md for available models, distance calculations, time calculations, density parameters, and neutrino effects.astropy.io.fits)references/fits.md for comprehensive file operations, header manipulation, image and table handling, multi-extension files, and performance considerations.astropy.table)references/tables.md for table creation, I/O operations, data manipulation, sorting, filtering, joins, grouping, and performance tips.astropy.time)references/time.md for time formats, time scales, conversions, arithmetic, observing features, and precision handling.astropy.wcs)references/wcs_and_other_modules.md for WCS operations and transformations.references/wcs_and_other_modules.md file also covers:# Reproducible install against the current stable release
uv pip install "astropy==7.2.0"
# Recommended optional dependencies for plotting and common workflows
uv pip install "astropy[recommended]==7.2.0"
# Full optional dependency set for broad astronomy workflows
uv pip install "astropy[all]==7.2.0"
[recommended] and [all] extras pull in transitive dependencies (matplotlib, scipy, etc.) at unpinned versions. For reproducible production environments, pin the full dependency tree with a lockfile (uv lock in a project, or uv pip compile for requirements files) and review the resolved versions before deploying.from astropy.coordinates import SkyCoord
import astropy.units as u
# Create coordinate
c = SkyCoord(ra='05h23m34.5s', dec='-69d45m22s', frame='icrs')
# Transform to galactic
c_gal = c.galactic
print(f"l={c_gal.l.deg}, b={c_gal.b.deg}")
# Transform to alt-az (requires time and location)
from astropy.time import Time
from astropy.coordinates import EarthLocation, AltAz
observing_time = Time('2023-06-15 23:00:00')
observing_location = EarthLocation(lat=40*u.deg, lon=-120*u.deg)
aa_frame = AltAz(obstime=observing_time, location=observing_location)
c_altaz = c.transform_to(aa_frame)
print(f"Alt={c_altaz.alt.deg}, Az={c_altaz.az.deg}")
from astropy.io import fits
import numpy as np
# Open FITS file
with fits.open('observation.fits') as hdul:
# Display structure
hdul.info()
# Get image data and header
data = hdul[1].data
header = hdul[1].header
# Access header values
exptime = header['EXPTIME']
filter_name = header['FILTER']
# Analyze data
mean = np.mean(data)
median = np.median(data)
print(f"Mean: {mean}, Median: {median}")
from astropy.cosmology import Planck18
import astropy.units as u
import numpy as np
# Calculate distances at z=1.5
z = 1.5
d_L = Planck18.luminosity_distance(z)
d_A = Planck18.angular_diameter_distance(z)
print(f"Luminosity distance: {d_L}")
print(f"Angular diameter distance: {d_A}")
# Age of universe at that redshift
age = Planck18.age(z)
print(f"Age at z={z}: {age.to(u.Gyr)}")
# Lookback time
t_lookback = Planck18.lookback_time(z)
print(f"Lookback time: {t_lookback.to(u.Gyr)}")
from astropy.table import Table
from astropy.coordinates import SkyCoord, match_coordinates_sky
import astropy.units as u
# Read catalogs
cat1 = Table.read('catalog1.fits')
cat2 = Table.read('catalog2.fits')
# Create coordinate objects
coords1 = SkyCoord(ra=cat1['RA']*u.degree, dec=cat1['DEC']*u.degree)
coords2 = SkyCoord(ra=cat2['RA']*u.degree, dec=cat2['DEC']*u.degree)
# Find matches
idx, sep, _ = coords1.match_to_catalog_sky(coords2)
# Filter by separation threshold
max_sep = 1 * u.arcsec
matches = sep < max_sep
# Create matched catalogs
cat1_matched = cat1[matches]
cat2_matched = cat2[idx[matches]]
print(f"Found {len(cat1_matched)} matches")
SkyCoord.from_name(), EarthLocation.of_site(refresh_cache=True), EarthLocation.of_address(), download_file(), remote FITS reads, and some IERS time/coordinate transforms can contact external services or update local caches. Avoid sending sensitive target names, addresses, URLs, or proprietary file locations to third-party services. When working with potentially sensitive targets or data locations, confirm with the user before making these network calls.astropy==7.2.0 for shared environments; update pins intentionally after reviewing release notes.astropy.cosmology submodule shims (astropy.cosmology.flrw, .core, .funcs, .connect, .parameter) are removed — import everything directly from astropy.cosmology (e.g., from astropy.cosmology import FlatLambdaCDM, z_at_value)astropy.constants defaults change from CODATA 2018 to CODATA 2022; pin a constants version via the astropyconst science states if reproducibility mattersastropy.test(), TestRunner) is formally deprecated — invoke pytest directly.loc element (t.loc["b", 2]) — use t.loc.with_index("b")[2] instead (removal planned for 9.0); astropy.utils.isiterable() — use numpy.iterable()(Bin)Table.update, _ExtensionHDU, _NonstandardExtHDU, and the tile_size argument for CompImageHDU; CompImageHeader is deprecated. Avoid those legacy patterns in new examples.recommended for common plotting/scientific dependencies and all only when a broad optional feature set is needed.references/units.md - Units, quantities, conversions, and equivalenciesreferences/coordinates.md - Coordinate systems, transformations, and catalog matchingreferences/cosmology.md - Cosmological models and calculationsreferences/fits.md - FITS file operations and manipulationreferences/tables.md - Table creation, I/O, and operationsreferences/time.md - Time formats, scales, and calculationsreferences/wcs_and_other_modules.md - WCS, NDData, modeling, visualization, constants, and utilities