Search code examples
units-of-measurementmetpy

AttributeError: module 'metpy.units' has no attribute 'knots'


I am trying to add basic units to my array to do calculations with Metpy. Using the function call metpy.units.meters (or .knots or .degrees) is not working.

Here is my code snippet:

import metpy.units as mpunits

...

wind_speed = webSKNT.values * mpunits.knots
wind_dir = webDRCT .values * mpunits.degrees
webu, webv = mpcalc.wind_components(wind_speed, wind_dir)

where webSKNT and webDRCT are pandas dataframes.

Here's the error I keep getting: AttributeError: module 'metpy.units' has no attribute 'knots'

and I get a similar error if I try: mpunits.meters


Solution

  • If you do the import like that, you need to do:

    mpunits.units.knots
    

    The canonical way we usually suggest to get the unit registry is:

    from metpy.units import units
    

    which then allows you to write:

    units.knots