hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.37k stars 466 forks source link

ureg.year returns a Unit but ureg['year'] returns a Quantity. Why? #1303

Open rjplevin opened 3 years ago

rjplevin commented 3 years ago
ureg['year']
Out[18]: 1 <Unit('year')>
ureg.year
Out[19]: <Unit('year')>

This behavior seems to violate reasonable assumptions (and differs from the behavior of pandas, where df['colname'] is the same as df.colname, presuming colname is a valid identifier.)

If this is intentional, it's unclear why it makes sense for ureg['year'] to return a Quantity.

jules-ch commented 3 years ago

If you look at the code, this use case has been deprecated:

    def __getitem__(self, item):
        logger.warning(
            "Calling the getitem method from a UnitRegistry is deprecated. "
            "use `parse_expression` method or use the registry as a callable."
        )
        return self.parse_expression(item)

parse_expression returns a Quantity.

I agree with you on this, and since this deprecation is like 5 years old, we can safely do something about it.

So we can either remove this function or change it.