Pandas DateTime Index leerer DatenframePython

Python-Programme
Anonymous
 Pandas DateTime Index leerer Datenframe

Post by Anonymous »

Ich habe den folgenden Code: < /p>

Code: Select all

data = pandas.read_csv('data.csv')
data['when'] = pandas.to_datetime(data['when'])
data.set_index('when', inplace=True)
print(data)
print(data.index.dtype)
< /code>
Was druckt: < /p>
                 price
when
2025-01-04  98259.4300
2025-01-03  98126.6400
2025-01-02  96949.1800
2025-01-01  94610.1400
2024-12-31  93647.0100
...                ...
2010-07-21      0.0792
2010-07-20      0.0747
2010-07-19      0.0808
2010-07-18      0.0858
2010-07-17      0.0500

[5286 rows x 1 columns]
datetime64[ns]
< /code>
Dann versuche ich, einen Bereich wie diesen auszuwählen: < /p>
start_date = datetime(year=2010,month=1,day=1)
end_date = datetime(year=2025,month=1,day=1)
print(data.loc[start_date:end_date])
print(data.loc[start_date:])
print(data.loc[:end_date])
< /code>
Und dies druckt < /p>
Empty DataFrame
Columns: [price]
Index: []
Empty DataFrame
Columns: [price]
Index: []
price
when
2025-01-04  98259.43
2025-01-03  98126.64
2025-01-02  96949.18
2025-01-01  94610.14
Warum?
Ich verwende Pandas 2.2.3.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post