mroberge/hydrofunctions

Pandas error: TypeError: Cannot compare type 'Timedelta' with type 'Minute'

craignwf opened this issue · 5 comments

  • HydroFunctions version: hydrofunctions==0.2.0
  • Python version: Python 3.6.5 :: Anaconda, Inc.
  • Operating System: Microsoft Windows 10 Pro

I just installed Hydrofunctions and tried the "Basic Usage" commands here: https://github.com/mroberge/hydrofunctions:

INPUT

import hydrofunctions as hf
%matplotlib inline
herring = hf.NWIS('01585200', 'iv', period='P55D')

OUTPUT

Requested data from https://waterservices.usgs.gov/nwis/iv/?format=json%2C1.1&sites=01585200&period=P55D

TypeError Traceback (most recent call last)
in ()
----> 1 herring = hf.NWIS('01585200', 'iv', period='P55D')

~\AppData\Roaming\Python\Python36\site-packages\hydrofunctions\station.py in init(self, site, service, start_date, end_date, stateCd, countyCd, bBox, parameterCd, period, file)
132 try:
133 self.json = self.response.json()
--> 134 self._dataframe, self.meta = hf.extract_nwis_df(self.json)
135 self.ok = self.response.ok
136 if file is not None:

~\AppData\Roaming\Python\Python36\site-packages\hydrofunctions\hydrofunctions.py in extract_nwis_df(nwis_dict, interpolate)
511 )
512 DF = DF[~DF.index.duplicated(keep="first")]
--> 513 if local_freq > to_offset("0min"):
514 local_clean_index = pd.date_range(
515 start=local_start, end=local_end, freq=local_freq, tz="UTC"

pandas_libs\tslibs\timedeltas.pyx in pandas._libs.tslibs.timedeltas._Timedelta.richcmp()

TypeError: Cannot compare type 'Timedelta' with type 'Minute'

I imagine this is a Pandas issue and I'll attempt to resolve, but I thought you may have run into this already.

Hello! Thank you for posting this.
This is a strange error- I'm using Windows 10 and Anaconda, and I wasn't able to replicate this in a Python 3.8 environment.
???

Since you have conda installed, let's try this out again in a clean environment and see if we can replicate the error.

Could you try doing the following from an anaconda command prompt? (you'll see a program icon for cmd.exe or PowerShell in the Start menu under Anaconda ::

conda create --name py38env python=3.8

Once it is done installing all of that, activate the new environment and install hydrofunctions:

activate py38env
pip install hydrofunctions

This will give you a new prompt showing your current directory and the name of your new environment (py38env), plus you will have hydrofunctions and all of its dependencies, like pandas. Now start python and try your commands out again::

python
import hydrofunctions as hf
herring = hf.NWIS('01585200', 'iv', period='P55D')

Let me know what you get!
My hope is that it will just print out the URL and no error messages!

If it works, you can::

herring.df()

or just::

herring

It works perfectly now! Thank you!

USGS:01585200: WEST BRANCH HERRING RUN AT IDLEWYLDE, MD

    00060: <5 * Minutes>  Discharge, cubic feet per second
    00065: <5 * Minutes>  Gage height, feet
Start: 2021-02-23 18:20:00+00:00
End:   2021-04-19 17:05:00+00:00

That's great!
I'm going to keep this issue open for a few more days while I check over the code. Thanks for opening this issue!
Let me know if you find hydrofunctions useful and if you have any ideas for applications, features, or additions to the user's manual.
-Marty

As it stands now, pandas is able to compare Timedeltas to Timedeltas, offsets to offsets, and Timedeltas to offsets. You can also create a new index using offsets or Timedeltas for the frequency.

Pandas uses offsets for the index.freq property and Timedeltas for when you subtract one time from another time.

In my code, I use offsets AND Timedeltas to store the frequency of a series.

  • the str() of an offset looks better than for a Timedelta, so I use them in NWIS.repr() to show the frequency.
  • In the past it was necessary to use to_offset() to convert strings to times. Now you can use pd.Timedelta('5min') or to_offset('5min') equally well.
  • calc_freq() returns a frequency as a Timedelta.

closed with 5b812e2