Can't index by timestamp in pandas dataframe

Delta_Fore

I took an excel sheet which has dates and some values and want to convert them to pandas dataframe and select only rows which are between certain dates.

For some reason I cannot select a row by date index

Raw Data in Excel file

MCU                         
Timestamp   50D 10P1    10P2    10P3    10P6    10P9    10P12
12-Feb-15   25.17   5.88    5.92    5.98    6.18    6.23    6.33
11-Feb-15   25.9    6.05    6.09    6.15    6.28    6.31    6.39
10-Feb-15   26.38   5.94    6.05    6.15    6.33    6.39    6.46

Code

xls = pd.ExcelFile('e:/Data.xlsx')
vols = xls.parse(asset.upper()+'VOL',header=1)
vols.set_index('Timestamp',inplace=True)

Data before set_index

      Timestamp    50D  10P1  10P2  10P3  10P6  10P9  10P12  25P1  25P2  \
0    2015-02-12  25.17  5.88  5.92  5.98  6.18  6.23   6.33  2.98  3.08   
1    2015-02-11  25.90  6.05  6.09  6.15  6.28  6.31   6.39  3.12  3.17   
2    2015-02-10  26.38  5.94  6.05  6.15  6.33  6.39   6.46  3.01  3.16  

Data after set_index

              50D  10P1  10P2  10P3  10P6  10P9  10P12  25P1  25P2  25P3  \
Timestamp                                                                  
2015-02-12  25.17  5.88  5.92  5.98  6.18  6.23   6.33  2.98  3.08  3.21   
2015-02-11  25.90  6.05  6.09  6.15  6.28  6.31   6.39  3.12  3.17  3.32   
2015-02-10  26.38  5.94  6.05  6.15  6.33  6.39   6.46  3.01  3.16  3.31  

Output

>>> vols.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-12, ..., NaT]
Length: 1478, Freq: None, Timezone: None

>>> vols[date(2015,2,12)]
*** KeyError: datetime.date(2015, 2, 12)

I would expect this not to fail, and also I should be able to select a range of dates. Tried so many combinations but not getting it.

Alex Riley

Using a datetime.date instance to try to retrieve the index won't work, you just need a string representation of the date, e.g. '2015-02-12' or '2015/02/14'.

Secondly, vols[date(2015,2,12)] is actually looking in your DataFrame's column headings, not the index. You can use loc to fetch row index labels instead. For example you could write vols.loc['2015-02-12']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I slice a dataframe by timestamp, when timestamp isn't classified as index?

From Dev

Timestamp index for pandas dataframe from read_json()

From Dev

How can I find the previous row in a DataFrame with a Timestamp index?

From Dev

Can't export pandas dataframe to excel / encoding

From Dev

Can't replace string content in pandas DataFrame

From Dev

Failing to convert Pandas dataframe timestamp

From Dev

pandas dataframe group by uneven timestamp

From Dev

How to fetch timestamp in Pandas DataFrame

From Dev

How to fetch timestamp in Pandas DataFrame

From Dev

How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

From Dev

Save pandas DataFrame in a way that preserves the type of the timestamp index (existing SO answer does not work?)

From Dev

How can I print out just the index of a pandas dataframe?

From Dev

How can I retrieve a row by index value from a Pandas DataFrame?

From Dev

How can I transform a dataframe in pandas without losing my index?

From Dev

How can I advance the index of a pandas.dataframe by one quarter?

From Dev

How can I use a combination or a string as an Index of DataFrame (Pandas)?

From Dev

Pandas DataFrame datetime index doesn't survive JSON conversion and reconversion

From Dev

Find row index of pandas dataframe that don't have finite values

From Dev

pandas dataframe by index and integer

From Dev

Print Pandas dataframe with index

From Java

Rename Pandas DataFrame Index

From Dev

Pandas DataFrame with continuous index

From Dev

Reassigning index in pandas DataFrame

From Dev

Reassigning index in pandas DataFrame

From Dev

Split pandas dataframe index

From Dev

Append DataFrame to an Index Pandas

From Dev

Returning index of DataFrame pandas

From Dev

Query Pandas Dataframe Index

From Dev

Pandas aligning multiple dataframes with TimeStamp index

Related Related

  1. 1

    How can I slice a dataframe by timestamp, when timestamp isn't classified as index?

  2. 2

    Timestamp index for pandas dataframe from read_json()

  3. 3

    How can I find the previous row in a DataFrame with a Timestamp index?

  4. 4

    Can't export pandas dataframe to excel / encoding

  5. 5

    Can't replace string content in pandas DataFrame

  6. 6

    Failing to convert Pandas dataframe timestamp

  7. 7

    pandas dataframe group by uneven timestamp

  8. 8

    How to fetch timestamp in Pandas DataFrame

  9. 9

    How to fetch timestamp in Pandas DataFrame

  10. 10

    How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

  11. 11

    Save pandas DataFrame in a way that preserves the type of the timestamp index (existing SO answer does not work?)

  12. 12

    How can I print out just the index of a pandas dataframe?

  13. 13

    How can I retrieve a row by index value from a Pandas DataFrame?

  14. 14

    How can I transform a dataframe in pandas without losing my index?

  15. 15

    How can I advance the index of a pandas.dataframe by one quarter?

  16. 16

    How can I use a combination or a string as an Index of DataFrame (Pandas)?

  17. 17

    Pandas DataFrame datetime index doesn't survive JSON conversion and reconversion

  18. 18

    Find row index of pandas dataframe that don't have finite values

  19. 19

    pandas dataframe by index and integer

  20. 20

    Print Pandas dataframe with index

  21. 21

    Rename Pandas DataFrame Index

  22. 22

    Pandas DataFrame with continuous index

  23. 23

    Reassigning index in pandas DataFrame

  24. 24

    Reassigning index in pandas DataFrame

  25. 25

    Split pandas dataframe index

  26. 26

    Append DataFrame to an Index Pandas

  27. 27

    Returning index of DataFrame pandas

  28. 28

    Query Pandas Dataframe Index

  29. 29

    Pandas aligning multiple dataframes with TimeStamp index

HotTag

Archive