简体   繁体   中英

Reading data from csv file into time series with pandas

My goal is to read EURUSD data (daily) into a time series object where I can easily slice-and-dice, aggregate, and resample the information based on irregular-ish time frames. This is most likely a simple answer. I'm working out of Python for Data Analysis but can't seem to bridge the gap.

After downloading and unzipping the data , I run the following code:

>>> import pandas as pd
>>> df = pd.read_csv('EURUSD_day.csv', parse_dates = {'Timestamp' : ['<DATE>', '<TIME>']}, index_col = 'Timestamp')

So far so good. I now have a nice data frame with Timestamps as the index.

However, the book implies (p. 295) that I should be able to subset the data, as follows, to look at all the data from the year 2001.

>>> df['2001']

But, that doesn't work.

Reading this question and answer tells me that I could import Timestamp:

>>> from pandas.lib import Timestamp
>>> s = df['<CLOSE>']

Which seems to work for a particular day:

>>> s[Timestamp('2001-01-04)]
0.9506999999

Yet, the following code yields a single value for my desired range of all data from year 2001.

>>> s[Timestamp('2001')]
0.8959

I know I am missing something simple, something basic. Can anyone help?

Thank you, Brian

The example on pg. 295 is being performed on Series object which is why indexing with the year works. With a DataFrame you would want df.ix['2001'] to achieve the same results.

If you want to get all of the columns, then df.ix['2001'] .

If you're interested only in "CLOSE", since you already did s = df['<CLOSE>'] , you can get the 2001 values by s['2001']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM