I have a csv file of index rebalance information (component x rebalance effective date) and want to determine the number of days in between each reblance date. I've gotten to the point where I've been able to isolate the header row and convert each date from string to datetime. I am having issues figuring out how select a specific item in the list to subtract it from another to cal the delta. When I try to select a specific item I get the following error:
"TypeError: list indices must be integers or slices, not tuple".
I have tried converting the datetime results to int
but I get:
"Attribute Error: 'list' object has no attribute 'astype'"
In other parts of my project I have simply indexed dates (dates in column[0]) and tested for location to pull the right data from other columns but that won't work here. I still need to find the delta(days) and convert that days number into an (int) so I can us it to iterate through the next step.
Here is the code to import and pull out the headers:
basketfile = "C:\\....\\Test_Baskets.csv"
rebaldates = pd.read_csv(basketfile, nrows=0)
newdates = [datetime.strptime(x, '%m/%d/%Y') for x in rebaldates]
this gives me:
In [141]: newdates
Out[141]:
[datetime.datetime(2010, 12, 31, 0, 0),
datetime.datetime(2007, 12, 31, 0, 0),
datetime.datetime(2008, 6, 20, 0, 0),
datetime.datetime(2008, 12, 19, 0, 0),
datetime.datetime(2009, 6, 19, 0, 0),
datetime.datetime(2009, 12, 18, 0, 0),
datetime.datetime(2010, 6, 18, 0, 0),
datetime.datetime(2010, 12, 17, 0, 0)]
I want to be able to use each element to calc the delta but I get this error when I try to select a specific element:
newdates[:, :8]
Traceback (most recent call last):
File "<ipython-input-142-34368864ad97>", line 1, in <module>
newdates[:, :8]
TypeError: list indices must be integers or slices, not tuple
I would expect that once the date strings have been converted to datetime I would be able to use them more like numbers but there is obviously another step I need to take in formatting either the items or how they are being housed. I'm just not sure what that is...