Skip to content

BUG: Cant access some TimedeltaProperties components directly #33255

Closed
@ShaharNaveh

Description

@ShaharNaveh

XREF: #33208 (comment)


Note:

I'm using "hours" here for the examples, but this applies to

  • hours
  • minutes
  • milliseconds

Short explanation:

Not all components are exposed directly:

Example:

In [1]: import pandas as pd                                                                                   

In [2]: s = pd.Series(pd.timedelta_range(start="1 day", periods=5, freq="H"))                                 

In [3]: s.dt.components                                                                                       
Out[3]: 
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0     1      0        0        0             0             0            0
1     1      1        0        0             0             0            0
2     1      2        0        0             0             0            0
3     1      3        0        0             0             0            0
4     1      4        0        0             0             0            0

In [4]: s.dt.days                                                                                             
Out[4]: 
0    1
1    1
2    1
3    1
4    1
dtype: int64

In [5]: s.dt.hours                                                                                            
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-86f203890f2a> in <module>
----> 1 s.dt.hours

AttributeError: 'TimedeltaProperties' object has no attribute 'hours'

Long explanation:

If we create a series that contains a timedelta, we can access only some of the components directly.

Example setup:

In [1]: import pandas as pd                                                                                   

In [2]: s = pd.Series(pd.timedelta_range(start="1 day", periods=5, freq="H"))                                 

In [3]: s                                                                                                     
Out[3]: 
0   1 days 00:00:00
1   1 days 01:00:00
2   1 days 02:00:00
3   1 days 03:00:00
4   1 days 04:00:00
dtype: timedelta64[ns]

In [4]: s.dt.components                                                                                       
Out[4]: 
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0     1      0        0        0             0             0            0
1     1      1        0        0             0             0            0
2     1      2        0        0             0             0            0
3     1      3        0        0             0             0            0
4     1      4        0        0             0             0            0

We can access "days" and "seconds" directly, for example:

In [5]: s.dt.days                                                                                             
Out[5]: 
0    1
1    1
2    1
3    1
4    1
dtype: int64

In [6]: s.dt.seconds                                                                                          
Out[6]: 
0        0
1     3600
2     7200
3    10800
4    14400
dtype: int64

But if we try to access "hours" for example directly:

In [7]: s.dt.hours                                                                                            
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-86f203890f2a> in <module>
----> 1 s.dt.hours

AttributeError: 'TimedeltaProperties' object has no attribute 'hours'

Output of pd.show_versions():

INSTALLED VERSIONS

commit : 37dc5dc
python : 3.7.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.5.13.a-1-hardened
Version : #1 SMP PREEMPT Wed, 25 Mar 2020 21:46:24 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0.dev0+1089.g37dc5dc39
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.0.0.post20200311
Cython : 0.29.16
pytest : 5.4.1
hypothesis : 5.8.0
sphinx : 2.4.4
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.3.2
fastparquet : 0.3.3
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.1
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pyxlsb : None
s3fs : 0.4.2
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.2.0
xlwt : 1.3.0
numba : 0.48.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions