forked from quandl/quandl-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_mixin.py
48 lines (40 loc) · 2.05 KB
/
data_mixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pandas as pd
from quandl.errors.quandl_error import ColumnNotFound
class DataMixin(object):
# DataFrame will respect order of input list of list
def to_pandas(self, keep_column_indexes=[]):
data = self.to_list()
# ensure pandas gets a list of lists
if data and isinstance(data, list) and not isinstance(data[0], list):
data = [data]
if 'columns' in self.meta.keys():
df = pd.DataFrame(data=data, columns=self.columns)
for index, column_type in enumerate(self.column_types):
if column_type == 'Date':
df[self.columns[index]] = df[self.columns[index]].apply(pd.to_datetime)
else:
df = pd.DataFrame(data=data, columns=self.column_names)
# ensure our first column of time series data is of pd.datetime
df[self.column_names[0]] = df[self.column_names[0]].apply(pd.to_datetime)
df.set_index(self.column_names[0], inplace=True)
# unfortunately to_records() cannot handle unicode in 2.7
df.index.name = str(df.index.name)
# keep_column_indexes are 0 based, 0 is the first column
if len(keep_column_indexes) > 0:
self._validate_col_index(df, keep_column_indexes)
# need to decrement all our indexes by 1 because
# Date is considered a column by our API, but in pandas,
# it is the index, so column 0 is the first column after Date index
keep_column_indexes = list([x - 1 for x in keep_column_indexes])
df = df.iloc[:, keep_column_indexes]
return df
def to_numpy(self):
return self.to_pandas().to_records()
def to_csv(self):
return self.to_pandas().to_csv()
def _validate_col_index(self, df, keep_column_indexes):
num_columns = len(df.columns)
for col_index in keep_column_indexes:
if col_index > num_columns or col_index < 1:
raise ColumnNotFound('Requested column index %s does not exist'
% col_index)