Skip to content

Fix dehumanize to handle decimal fractions#1257

Open
rstar327 wants to merge 2 commits intoarrow-py:masterfrom
rstar327:fix-dehumanize-decimals
Open

Fix dehumanize to handle decimal fractions#1257
rstar327 wants to merge 2 commits intoarrow-py:masterfrom
rstar327:fix-dehumanize-decimals

Conversation

@rstar327
Copy link

Pull Request Checklist

  • 🧪 Added tests for changed code.
  • 🛠️ All tests pass when run locally (run tox or make test to find out!).
  • 🧹 All linting checks pass when run locally (run tox -e lint or make lint to find out!).
  • 📚 Updated documentation for changed code.
  • ⏩ Code is up-to-date with the master branch.

Description of Changes

Closes: #1237

dehumanize() currently only matches integer values in time strings, so inputs like "3.5 hours ago" are parsed incorrectly — the decimal portion is silently ignored, treating 3.5 as 3.

This PR updates the number matching regex to support decimal fractions using either . or , as the decimal separator, and parses values as float instead of int:

# Before
num_pattern = re.compile(r"\d+")
change_value = int(num_match.group())

# After
num_pattern = re.compile(r"\d+(?:[.,]\d+)?")
change_value = float(num_match.group().replace(",", "."))

Example:

>>> arrow.get("2025-12-10T09:00:00").dehumanize("2 days 3.5 hours ago")
# Before: <Arrow [2025-12-08T06:00:00+00:00]>  (3.5 truncated to 3)
# After:  <Arrow [2025-12-08T05:30:00+00:00]>  (correct)

Tests added:

  • Decimal hours (. separator)
  • Decimal with multiple units ("2 days 3.5 hours ago")
  • Comma decimal separator ("3,5 hours ago")
  • Decimal in future strings ("in 1.5 hours")
  • Decimal minutes
  • Integer values still work correctly

All 1909 tests pass with 99.93% coverage.

@codecov
Copy link

codecov bot commented Feb 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b423717) to head (24e45be).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1257   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           10        10           
  Lines         2315      2315           
  Branches       358       358           
=========================================
  Hits          2315      2315           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make dehumanize() handle values with decimal fractions correctly

1 participant