Ben.hardy
Ben.hardy

Reputation: 125

TypeError: 'Timestamp' object is not subscriptable

I was visualising a dataset of election polls and requires to use the data of october 2012 but its giving me an error.

import pandas as pd
from pandas import Series,DataFrame
import numpy as np
poll_df=pd.read_csv('http://elections.huffingtonpost.com/pollster/2012-general-election-romney-vs-obama.csv')
row_in=0
xlimit=[]
for date in poll_df['Start Date']:
    if date[0:7] == '2012-10':
        xlimit.append(row_in)
        row_in += 1
    else:
        row_in+=1
print(min(xlimit))
print(max(xlimit))

why is it giving me this error and what does it mean?

Upvotes: 1

Views: 6196

Answers (1)

jezrael
jezrael

Reputation: 862921

Use:

url ='http://elections.huffingtonpost.com/pollster/2012-general-election-romney-vs-obama.csv'

Solution with datetimes - convert column to dates in read_csv and then compare strings by strftime and filter by boolean indexing:

poll_df = pd.read_csv(url, parse_dates=['Start Date'])

df = poll_df[poll_df['Start Date'].dt.strftime('%Y-%m') == '2012-10']

print(df['Start Date'].dtype)
datetime64[ns]

Solution with strings - extract first 7 values by indexing with str:

poll_df = pd.read_csv(url)

df = poll_df[poll_df['Start Date'].str[:7] == '2012-10']

print(df['Start Date'].dtype)
object

print(df.head())

                Pollster Start Date    End Date  Entry Date/Time (ET)  \
18                YouGov 2012-10-31  2012-11-03  2012-11-04T16:24:50Z   
19                   Pew 2012-10-31  2012-11-03  2012-11-04T15:46:59Z   
21             Rasmussen 2012-10-31  2012-11-02  2012-11-03T10:54:09Z   
22     Purple Strategies 2012-10-31  2012-11-01  2012-11-02T12:31:41Z   
23  JZ Analytics/Newsmax 2012-10-30  2012-11-01  2012-11-02T22:57:27Z   

    Number of Observations     Population             Mode  Obama  Romney  \
18                 36472.0  Likely Voters         Internet   49.0    47.0   
19                  2709.0  Likely Voters       Live Phone   48.0    45.0   
21                  1500.0  Likely Voters  Automated Phone   48.0    48.0   
22                  1000.0  Likely Voters       IVR/Online   47.0    46.0   
23                  1030.0  Likely Voters         Internet   48.0    46.0   

    Undecided  Other                                       Pollster URL  \
18        3.0    NaN  http://elections.huffingtonpost.com/pollster/p...   
19        NaN    3.0  http://elections.huffingtonpost.com/pollster/p...   
21        2.0    1.0  http://elections.huffingtonpost.com/pollster/p...   
22        7.0    NaN  http://elections.huffingtonpost.com/pollster/p...   
23        6.0    NaN  http://elections.huffingtonpost.com/pollster/p...   

                                           Source URL     Partisan  \
18  http://cdn.yougov.com/r/1/ygTabs_november_like...  Nonpartisan   
19  http://www.people-press.org/2012/11/04/obama-g...  Nonpartisan   
21  http://www.rasmussenreports.com/public_content...  Nonpartisan   
22  http://www.purplestrategies.com/wp-content/upl...  Nonpartisan   
23                        http://www.jzanalytics.com/      Sponsor   

   Affiliation  Question Text  Question Iteration  
18        None            NaN                   1  
19        None            NaN                   1  
21        None            NaN                   1  
22        None            NaN                   1  
23         Rep            NaN                   1  

Upvotes: 1

Related Questions