Computing a panchang in Python: what building it yourself involves
October 9, 2026 · Updated November 9, 2026
You can compute a tithi in Python in about forty lines. You cannot ship a correct panchang in forty lines. This post shows the first part, so you can see the core is not magic, and then lists what separates a demo from something you can show to families who will notice a wrong date.
We are building TithiKit, a Hindu calendar API, so we have a stake in the "buy" answer. We have tried to be fair: for some projects, building is the right call.
The core: elongation and sunrise
A tithi is defined by the elongation, the Moon's ecliptic longitude minus the Sun's, in 12° steps. The day is named after the tithi in effect at local sunrise. So you need two things: Sun and Moon positions, and sunrise. (Background: how a tithi is calculated.)
Skyfield is an MIT-licensed Python astronomy library that reads NASA JPL ephemeris files. DE440s covers 1849 to 2150 in about 32 MB.
pip install skyfieldStep 1: load the ephemeris
from skyfield.api import load, wgs84
from skyfield import almanac
ts = load.timescale()
eph = load("de440s.bsp") # downloads the file on first run
earth, sun, moon = eph["earth"], eph["sun"], eph["moon"]Step 2: elongation and tithi index
def elongation(t):
e = earth.at(t)
_, sun_lon, _ = e.observe(sun).apparent().ecliptic_latlon(epoch="date")
_, moon_lon, _ = e.observe(moon).apparent().ecliptic_latlon(epoch="date")
return (moon_lon.degrees - sun_lon.degrees) % 360
def tithi_index(t):
"""0 to 29. Add 1 for the conventional tithi number."""
return (elongation(t) // 12).astype(int)
tithi_index.step_days = 0.25 # tithis last ~20+ hours; sample every 6 hNo ayanamsa appears here. The tithi uses a difference of two longitudes, so the tropical-to-sidereal offset cancels.
Step 3: find every tithi transition in a range
t0 = ts.utc(2027, 1, 1)
t1 = ts.utc(2027, 2, 1)
times, indices = almanac.find_discrete(t0, t1, tithi_index)
for t, i in zip(times, indices):
print(t.utc_strftime("%Y-%m-%d %H:%M UTC"), "-> tithi", i + 1)find_discrete searches for the instants where the function's value changes, and refines each to high precision.
Step 4: sunrise for a location
place = wgs84.latlon(40.7128, -74.0060) # New York
observer = earth + place
rises, _ = almanac.find_risings(observer, sun, t0, t1, horizon_degrees=-0.8333)The horizon of −0.8333° corresponds to the upper limb of the Sun with 34′ of standard refraction (34′ + about 16′ for half the disc = 50′). That is one definition of sunrise among several; see which sunrise?.
Step 5: tithi at sunrise, in local time
from zoneinfo import ZoneInfo
ny = ZoneInfo("America/New_York")
for t in rises:
local = t.utc_datetime().astimezone(ny)
print(local.strftime("%Y-%m-%d %H:%M %Z"), "tithi", int(tithi_index(t)) + 1)That is a working, location-aware tithi calendar. Note what we did right: we stored a zone name, not an offset, and converted each instant separately, so daylight saving is handled by the IANA database.
The hard parts that come next
1. Performance
Computing positions per request is slow if you serve many users. Precompute every location-independent instant (tithi, nakshatra, yoga and karana boundaries, sankrantis, new and full moons) for your date range, store it as a table, and compute only sunrise per request. That is the design TithiKit is built on; see the methodology.
2. Ayanamsa
Nakshatras, yogas, sankrantis and solar months need sidereal longitudes, so you need an ayanamsa. Skyfield does not ship Lahiri. You must implement it from a cited definition and test it against published values. One arcminute of error moves a sankranti by about 24 minutes. See Lahiri ayanamsa explained.
3. Months and leap months
Month names come from new moons and sankrantis. You need both amanta and purnimanta naming, adhika and kshaya months, and the samvatsara. See the leap month post.
4. Skipped and repeated tithis
Your output must represent days with two tithi changes and tithis spanning two sunrises. Most demo code silently drops them. See skipped and repeated tithis in your UI.
5. Festivals
This is the largest piece of work. Each festival has its own rule: which tithi, at which local moment (sunrise, after sunset, midnight), with tradition-specific variants and tie-breaks for skipped or repeated tithis. Every rule needs a written source. Expect to spend more time on citations than on code.
6. Regional solar calendars
Tamil, Malayalam, Bengali and Odia calendars start months at sankrantis, with different rules for which civil day counts as day one. Each needs its own rule and its own test cases.
7. Edge cases
DST gaps and overlaps, locations near the date line, and high latitudes with no sunrise. Decide a policy for each and write it down. The DST guide lists test cases.
8. Tests against real almanacs
Comparing your code with another library proves little if both share assumptions. You need reference cases transcribed from printed almanacs, with the source cited, and a way to record when two references disagree.
9. Licensing
Check the license of every library you depend on. Some widely used astronomy libraries are available under copyleft licenses with obligations for hosted services, or under paid commercial licenses. Skyfield is MIT-licensed. Check the terms of any ephemeris file you redistribute.
When to build it yourself
Build when:
- you need only tithi and sunrise for a handful of locations;
- you have someone who can verify output against almanacs for your tradition;
- you want to learn the astronomy, which is genuinely fun;
- your volume is high enough that an API bill outweighs engineering time.
Use an API when:
- you serve users in many cities and timezones;
- you need festival dates with rules for several traditions;
- you cannot spare time to maintain citations and tests;
- you want someone else to take bug reports about edge cases.
Where TithiKit fits
TithiKit is a planned API that does the steps above for any coordinates and timezone, returns the rule behind each festival date, and is tested in public. It is in development. If you would rather not maintain all of this, join early access. If you build it yourself, we hope this list saves you some surprises.