question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pandas.Series.dt.total_seconds() documentation confusing

See original GitHub issue

Code Sample, a copy-pastable example if possible

import pandas as pd

series = pd.Series(pd.datetime(2017, 10, 30))

# Try with regular datetime Series (will fail)
series.dt.total_seconds()
Output: 
AttributeError: 'DatetimeProperties' object has no attribute 'total_seconds'
# Try with TimeDelta Series (intended use)
series.sub(pd.datetime(2017, 10, 29)).dt.total_seconds()
Output:
0    86400.0
dtype: float64

Problem description

The documentation for pd.Series.dt.total_seconds() is a bit confusing. Reading it, one would expect that this method should work on any Series with the dt accessor. This is incorrect; looking at the API reference for datetimeline properties, it’s made clear that this attribute is only available on timedelta Series.

It might be helpful to make it more clear in the documentation for pd.Series.dt.total_seconds() that the method is only available on timedelta Series. Most people will get to the page by googling “Pandas total_seconds” or something similar; they won’t often see where it falls in the API reference.

Is there any precedence for these kinds of heads-ups? I’m happy to make the documentation change and would like to make sure I follow any existing conventions (if they exist).

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.6.3.final.0 python-bits: 64 OS: Darwin OS-release: 16.7.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.21.0 pytest: None pip: 9.0.1 setuptools: 36.6.0 Cython: None numpy: 1.13.3 scipy: 1.0.0 pyarrow: None xarray: None IPython: 6.2.1 sphinx: None patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 2.1.0 openpyxl: None xlrd: 1.1.0 xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: 1.0b10 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.9.6 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
TomAugspurgercommented, Nov 2, 2017

Hmm, what about just slipping the type in the short description then? Maybe:

     def total_seconds(self):
         """
-        Total duration of each element expressed in seconds.
+        Total duration of each timedelta expressed in seconds.
         """
         return Index(self._maybe_mask_results(1e-9 * self.asi8),
                      name=self.name)

1reaction
JesperDramschcommented, Sep 1, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.Series.dt.total_seconds — pandas 1.5.2 documentation
Return total duration of each element expressed in seconds. This method is available directly on TimedeltaArray, TimedeltaIndex and on Series containing ...
Read more >
Pandas Series.dt.total_seconds() not found - Stack Overflow
total_seconds is a member of timedelta not datetime. Hence the error. You maybe be wanting dt.second. This returns the second component ...
Read more >
datetime — Basic date and time types — Python 3.11.1 ...
Returns a string representation of the timedelta object as a constructor call with ... (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds().
Read more >
Time and time zones — pvlib-python 0.3.0 documentation
pvlib-python relies on pandas and pytz to handle time and time zones. Therefore, the vast majority of the information in this document applies...
Read more >
Pandas time difference between columns in seconds
In pandas, you can view dtypes of a DataFrame, and in Spark, Now, let's see the content of the second_DataFrame. to_datetime (). g....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found