pythonpandasgithubmetpy

How to resolve 'module 'metpy' has no attribute 'calc'' error when using MetPy in Python?


I have got this error with metpy:

AttributeError: module 'metpy' has no attribute 'calc'

The code is as follows;

import metpy as mp
import numpy as np
from metpy.units import units

df = pd.read_csv('wind.csv', header = 0)
u = (df['u10'].to_numpy()) *units("m/s")
v = (df['v10'].to_numpy()) *units("m/s")
df['speed'] = mp.calc.wind_speed(u, v) 

Versions: numpy 1.23.5 metpy 1.5.0

Any idea how to solve it? Thank you.


Solution

  • To keep imports as quick as possible and streamlined, import metpy does not import the subpackages, it essentially only sets up xarray intergrations. If you want to use metpy.calc, you need to do import metpy.calc:

        import metpy.calc as mpcalc
        from metpy.units import units
        import pandas as pd
    
        df = pd.read_csv('wind.csv', header = 0)
        u = (df['u10'].to_numpy()) * units("m/s")
        v = (df['v10'].to_numpy()) * units("m/s")
        speed = mpcalc.wind_speed(u, v)