2

So I have dataframe with this index

print df.index

result:

<class 'pandas.tseries.index.DatetimeIndex'>
[2000-01-01 11:30:16.624989600, ..., 2000-01-01 11:50:33.124011100]
Length: 240178, Freq: None, Timezone: None

And I want to round seconds to for example:

<class 'pandas.tseries.index.DatetimeIndex'>
[2000-01-01 11:30:16.625, ..., 2000-01-01 11:50:33.124]
Length: 240178, Freq: None, Timezone: None

Or

<class 'pandas.tseries.index.DatetimeIndex'>
[2000-01-01 11:30:16.625000000, ..., 2000-01-01 11:50:33.124000000]
Length: 240178, Freq: None, Timezone: None

So I am using datetimeindex.round() method

print df.index.round(decimals=3)

with such result:

TypeError
Traceback (most recent call last) <ipython-input-9-6e2dbd74ae75> in <module>()
      1 print df.index
----> 2 print df.index.round(decimals=3) TypeError: ufunc multiply cannot use operands with types dtype('<M8[ns]') and dtype('float64')

Is that method simply not available yet?

EDIT:

pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 37 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.14.0
nose: 1.3.1
Cython: 0.20.1
numpy: 1.8.1
scipy: 0.13.3
statsmodels: 0.5.0
IPython: 2.0.0
sphinx: 1.2.2
patsy: 0.2.1
scikits.timeseries: None
dateutil: 2.2
pytz: 2013.9
bottleneck: None
tables: 3.1.1
numexpr: 2.3.1
matplotlib: 1.3.1
openpyxl: None
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
bq: None
apiclient: None
rpy2: None
sqlalchemy: 0.9.4
pymysql: None
psycopg2: None
ase
  • 13,231
  • 4
  • 34
  • 46
  • 1
    See these two answers - [here](http://stackoverflow.com/questions/17781159/round-pandas-datetime-index) and [here](http://stackoverflow.com/questions/13785932/how-to-round-a-pandas-datetimeindex). You wanna play around with precision a bit (e.g. use 1e6 instead of 1e9, or use `decimals` in the `round` method). I couldn't get exactly 625 milliseconds for your first index entry, but could get 62 or 624999936 (rounding errors most likely). – abudis Jun 17 '14 at 09:09
  • @abudis i have seen those answers, i'd really like to do it with that precision that i mentioned, also i know that it is possible to be done that way, but i'm wondering why that method is not working? – user3733710 Jun 17 '14 at 11:52
  • `round` does not work with a DatetimeIndex, you have to use one of the methods @abudis linked to. – joris Jun 17 '14 at 12:04
  • @joris so, should i report it as an issue or is it known fact ? Because that method is in API: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.round.html#pandas.DatetimeIndex.round – user3733710 Jun 17 '14 at 12:36
  • There is already an issue for enhancement proposal (https://github.com/pydata/pandas/issues/4314), for now it is a 'known fact' (round does only work for numerical values). But if you don't find it clear from the docs, you can always open an issue about that to improve the docs (or do a PR). – joris Jun 17 '14 at 12:48

0 Answers0