# import the dataset
from quantopian.interactive.data.eventvestor import stock_splits
# or if you want to import the free dataset, use:
# from quantopian.data.eventvestor import stock_splits_free

# import data operations
from odo import odo
# import other libraries we will use
import pandas as pd

# Let's use blaze to understand the data a bit using Blaze dshape()
stock_splits.dshape

# And how many rows are there?
# N.B. we're using a Blaze function to do this, not len()
stock_splits.count()

# Let's see what the data looks like. We'll grab the first three rows.
stock_splits[:3]

"""
Explanation: EventVestor: Stock Splits
In this notebook, we'll take a look at EventVestor's Stock Splits dataset, available on the Quantopian Store. This dataset spans January 01, 2007 through the current day, and documents stock splits and reverse stock splits.
Blaze
Before we dig into the data, we want to tell you about how  you generally access Quantopian Store data sets. These datasets are available through an API service known as Blaze. Blaze provides the Quantopian user with a convenient interface to access very large datasets.
Blaze provides an important function for accessing these datasets. Some of these sets are many millions of records. Bringing that data directly into Quantopian Research directly just is not viable. So Blaze allows us to provide a simple querying interface and shift the burden over to the server side.
It is common to use Blaze to reduce your dataset in size, convert it over to Pandas and then to use Pandas for further computation, manipulation and visualization.
Helpful links:
* Query building for Blaze
* Pandas-to-Blaze dictionary
* SQL-to-Blaze dictionary.
Once you've limited the size of your Blaze object, you can convert it to a Pandas DataFrames using:

from odo import odo
odo(expr, pandas.DataFrame)

Free samples and limits
One other key caveat: we limit the number of results returned from any given expression to 10,000 to protect against runaway memory usage. To be clear, you have access to all the data server side. We are limiting the size of the responses back from Blaze.
There is a free version of this dataset as well as a paid one. The free one includes about three years of historical data, though not up to the current day.
With preamble in place, let's get started:
End of explanation
"""


# get apple's sid first
nike_sid = symbols('NKE').sid
splits = stock_splits[(stock_splits.sid == nike_sid)]
# When displaying a Blaze Data Object, the printout is automatically truncated to ten rows.
splits.sort('asof_date')

"""
Explanation: Let's go over the columns:
- event_id: the unique identifier for this event.
- asof_date: EventVestor's timestamp of event capture.
- trade_date: for event announcements made before trading ends, trade_date is the same as event_date. For announcements issued after market close, trade_date is next market open day.
- symbol: stock ticker symbol of the affected company.
- event_type: this should always be Stock Split.
- event_headline: a brief description of the event
- split_type: stock split or reverse split
- split_factor: the x-for-y split factor. This is equivalently expressed by new_shares and old_shares.
- new_shares: number of new shares for x number of old shares
- old_shares: number of old shares exchanged for the number of new shares.
- effective_date: effective date of stock split.
- event_rating: this is always 1. The meaning of this is uncertain.
- timestamp: this is our timestamp on when we registered the data.
- sid: the equity's unique identifier. Use this instead of the symbol.
We've done much of the data processing for you. Fields like timestamp and sid are standardized across all our Store Datasets, so the datasets are easy to combine. We have standardized the sid across all our equity databases.
We can select columns and rows with ease. Below, we'll fetch Nike's stock splits.
End of explanation
"""


reverse = stock_splits[stock_splits.split_type == "Reverse Split"]
df = odo(reverse, pd.DataFrame)
df = df[['asof_date','split_factor','sid']]
df = df[df.sid.notnull()]
# When printing a pandas DataFrame, the head 30 and tail 30 rows are displayed. The middle is truncated.
df

"""
Explanation: Now suppose we want a DataFrame of stock_splits, but limited to reverse splits only. Of those, we then want to display the split_factor, timestamp, and sid.
End of explanation
"""


