Pandas/python and working with a column, in a dataframe, with a date
<p>I am currently working on a Python/Pandas data science project for fun. The data that I am looking at has a Date column where the date looks like the following: 2016-07-16. The data type is also an object. What I want to do is go through each date and pull data from across that row. Now, some rows may have the same date because two separate attacks occurred on that date. (I am looking at terrorism data.) What I currently have done is the following:</p>

<pre><code>dates = []
start = 0;
while start &lt; 300: 
    date = data.iat[start, 1]
    dates.append(date)
    start += 1
</code></pre>

<p>This will get me ALMOST what I want. However, I have two problems, the start variable is set to 0 but I cannot go to 365 since, like I said, each date may have multiple attacks. So one year may have like 400 attacks. Is there a way that I could end the data collection at 2016-12-31 or 2017-01-01 for example? Basically, is there a way to quickly determine the number of attacks, per year for year after year? Thank you for any help! </p>

<p>Oh I will say that I was trying something like:</p>

<pre><code>newDate = pd.to_datetime(startdate) + pd.DateOffset(days=1)
</code></pre>

<p>or </p>

<pre><code>data['Date']) + timedelta(days=1)
</code></pre>

<p>to add one to the date to end at the year. Not getting what I wanted plus, there could be more than one entry per day. </p>

<p>to explain further I could have something like this:</p>

<pre><code>Date            Deaths     Country 
2002-01-01         2         India 
2002-01-02         0         Pakistan
2001-01-02         1         France
</code></pre>

<p>The data has about 20,000 points and I need to find a way to stop it at the end of each year. That is my main issue. I cannot go to 365 because there may be multiple terrorist attacks on the same date around the world. </p>

<p>IMO there is no need to add a new column:</p>

<pre><code>In [132]: df
Out[132]:
        Date  Deaths   Country
0 2002-01-01       2     India
1 2002-01-02       0  Pakistan
2 2001-01-02       1    France

In [217]: df.groupby(df.Date.dt.year)['Deaths'].sum()
Out[217]:
Date
2001    1
2002    2
Name: Deaths, dtype: int64
</code></pre>

<p>or:</p>

<pre><code>In [218]: df.groupby(pd.TimeGrouper(freq='AS', key='Date'))['Deaths'].sum()
Out[218]:
Date
2001-01-01    1
2002-01-01    2
Freq: AS-JAN, Name: Deaths, dtype: int64

In [219]: df.groupby(pd.TimeGrouper(freq='A', key='Date'))['Deaths'].sum()
Out[219]:
Date
2001-12-31    1
2002-12-31    2
Freq: A-DEC, Name: Deaths, dtype: int64
</code></pre>

<p>and you can always access different parts (year, month, day, weekday, hour, etc.) of your DateTime column:</p>

<pre><code>In [137]: df.Date.dt.year
Out[137]:
0    2002
1    2002
2    2001
Name: Date, dtype: int64

In [138]: df.Date.dt.
df.Date.dt.ceil             df.Date.dt.freq             df.Date.dt.microsecond      df.Date.dt.strftime         df.Date.dt.weekday
df.Date.dt.date             df.Date.dt.hour             df.Date.dt.minute           df.Date.dt.time             df.Date.dt.weekday_name
df.Date.dt.day              df.Date.dt.is_month_end     df.Date.dt.month            df.Date.dt.to_period        df.Date.dt.weekofyear
df.Date.dt.dayofweek        df.Date.dt.is_month_start   df.Date.dt.nanosecond       df.Date.dt.to_pydatetime    df.Date.dt.year
df.Date.dt.dayofyear        df.Date.dt.is_quarter_end   df.Date.dt.normalize        df.Date.dt.tz
df.Date.dt.days_in_month    df.Date.dt.is_quarter_start df.Date.dt.quarter          df.Date.dt.tz_convert
df.Date.dt.daysinmonth      df.Date.dt.is_year_end      df.Date.dt.round            df.Date.dt.tz_localize
df.Date.dt.floor            df.Date.dt.is_year_start    df.Date.dt.second           df.Date.dt.week
</code></pre>

python date pandas