meteostat / meteostat-python

Access and analyze historical weather and climate data with Python.
https://dev.meteostat.net/python/
MIT License
437 stars 60 forks source link

interpolation error "ValueError: The name station occurs multiple times, use a level number" #130

Closed aldder closed 1 year ago

aldder commented 1 year ago

Hello, If you try to run the code in the interpolation documentation section it will output the following error:

some infos: python version: 3.11.4 meteostat: 1.6.5 pandas: 2.0.3

CODE:

from datetime
 import datetime
from meteostat import Stations, Hourly

stations = Stations()
stations = stations.nearby(49.2497, -123.1193)
station = stations.fetch(1)

start = datetime(2018, 1, 1)
end = datetime(2018, 12, 31, 23, 59)

data = Hourly(station, start=start, end=end)
data = data.normalize()
data = data.interpolate()
data = data.fetch()

print(data)

OUTPUT:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 14
     12 data = data.normalize()
     13 data = data.interpolate()
---> 14 data = data.fetch()
     16 print(data)

File [c:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\meteostat\series\fetch.py:25](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/meteostat/series/fetch.py:25), in fetch(self)
     23 # Remove station index if it's a single station
     24 if len(self._stations) == 1 and "station" in temp.index.names:
---> 25     temp = temp.reset_index(level="station", drop=True)
     27 # Return data frame
     28 return temp

File [c:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py:6162](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/pandas/core/frame.py:6162), in DataFrame.reset_index(self, level, drop, inplace, col_level, col_fill, allow_duplicates, names)
   6160 if not isinstance(level, (tuple, list)):
   6161     level = [level]
-> 6162 level = [self.index._get_level_number(lev) for lev in level]
   6163 if len(level) < self.index.nlevels:
   6164     new_index = self.index.droplevel(level)

File [c:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py:6162](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/pandas/core/frame.py:6162), in (.0)
   6160 if not isinstance(level, (tuple, list)):
   6161     level = [level]
-> 6162 level = [self.index._get_level_number(lev) for lev in level]
   6163 if len(level) < self.index.nlevels:
   6164     new_index = self.index.droplevel(level)

File [c:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\multi.py:1484](file:///C:/Users/user/AppData/Local/Programs/Python/Python311/Lib/site-packages/pandas/core/indexes/multi.py:1484), in MultiIndex._get_level_number(self, level)
   1482 count = self.names.count(level)
   1483 if (count > 1) and not is_integer(level):
-> 1484     raise ValueError(
   1485         f"The name {level} occurs multiple times, use a level number"
   1486     )
   1487 try:
   1488     level = self.names.index(level)

ValueError: The name station occurs multiple times, use a level number
peterspat commented 1 year ago

Unfortunately, I have the same problem and can confirm the issue for the Python and packages version.

BrushXue commented 1 year ago

I have the same problem with the same version.

clampr commented 1 year ago

Hi all,

It seems like Meteostat doesn't support Pandas v2. Version 1.6.6 has the correct dependency.