Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 2 support #4

Merged
merged 4 commits into from
Mar 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: python
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
Expand Down
4 changes: 2 additions & 2 deletions examples/speed_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
}
],
"source": [
"results.print()"
"results.show()"
]
},
{
Expand Down Expand Up @@ -248,7 +248,7 @@
}
],
"source": [
"results.print()"
"results.show()"
]
},
{
Expand Down
32 changes: 16 additions & 16 deletions examples/usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
}
],
"source": [
"df.print()"
"df.show()"
]
},
{
Expand Down Expand Up @@ -273,7 +273,7 @@
"source": [
"#indexes can be any non-repeating unique values\n",
"df.index = ['apple', 'pear', 7.7]\n",
"df.print()"
"df.show()"
]
},
{
Expand Down Expand Up @@ -417,7 +417,7 @@
"source": [
"# set a single cell\n",
"df.set(10, 'a', 100)\n",
"df.print()"
"df.show()"
]
},
{
Expand All @@ -443,7 +443,7 @@
"source": [
"# set a value outside current range creates a new row and/or column. Can also use [] for setting\n",
"df[13, 'c'] = 9\n",
"df.print()"
"df.show()"
]
},
{
Expand All @@ -469,7 +469,7 @@
"source": [
"# set column\n",
"df['b'] = 55\n",
"df.print()"
"df.show()"
]
},
{
Expand Down Expand Up @@ -656,7 +656,7 @@
],
"source": [
"# get an entire column\n",
"df['c'].print()"
"df['c'].show()"
]
},
{
Expand All @@ -682,7 +682,7 @@
],
"source": [
"# get list of columns\n",
"df[['a', 'c']].print()"
"df[['a', 'c']].show()"
]
},
{
Expand All @@ -706,7 +706,7 @@
],
"source": [
"# get subset of the index\n",
"df[[11, 12, 13], 'b'].print()"
"df[[11, 12, 13], 'b'].show()"
]
},
{
Expand All @@ -730,7 +730,7 @@
],
"source": [
"# get using slices\n",
"df[11:13, 'b'].print()"
"df[11:13, 'b'].show()"
]
},
{
Expand All @@ -753,7 +753,7 @@
],
"source": [
"# get a matrix\n",
"df[10:11, ['a', 'c']].print()"
"df[10:11, ['a', 'c']].show()"
]
},
{
Expand Down Expand Up @@ -830,7 +830,7 @@
}
],
"source": [
"df.get_locations(locations=[0, 2]).print()"
"df.get_locations(locations=[0, 2]).show()"
]
},
{
Expand All @@ -856,7 +856,7 @@
],
"source": [
"df.set_locations(locations=[0, 2], column='a', values=-9)\n",
"df.print()"
"df.show()"
]
},
{
Expand Down Expand Up @@ -886,7 +886,7 @@
}
],
"source": [
"df.head(2).print()"
"df.head(2).show()"
]
},
{
Expand All @@ -908,7 +908,7 @@
}
],
"source": [
"df.tail(2).print()"
"df.tail(2).show()"
]
},
{
Expand Down Expand Up @@ -1232,7 +1232,7 @@
],
"source": [
"df1 = rc.DataFrame({'a': [1, 2], 'b': [5, 6]}, index=[1, 2])\n",
"df1.print()"
"df1.show()"
]
},
{
Expand Down Expand Up @@ -1920,7 +1920,7 @@
}
],
"source": [
"df.print()"
"df.show()"
]
},
{
Expand Down
31 changes: 23 additions & 8 deletions raccoon/dataframe.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""
DataFrame class
"""

from __future__ import print_function
import sys
from itertools import compress
from collections import OrderedDict, namedtuple
from bisect import bisect_left, bisect_right
from tabulate import tabulate
from blist import blist

PYTHON3 = (sys.version_info >= (3, 0))

try:
import simplejson as json
except ImportError:
Expand Down Expand Up @@ -133,7 +136,7 @@ def _make_table(self, index=True, **kwargs):
kwargs['headers'] = 'keys' if 'headers' not in kwargs.keys() else kwargs['headers']
return tabulate(self.to_dict(ordered=True, index=index), **kwargs)

def print(self, index=True, **kwargs):
def show(self, index=True, **kwargs):
"""
Print the contents of the DataFrame. This method uses the tabulate function from the tabulate package. Use the
kwargs to pass along any arguments to the tabulate function.
Expand Down Expand Up @@ -176,11 +179,17 @@ def __len__(self):

@property
def data(self):
return self._data.copy()
if PYTHON3:
return self._data.copy()
else:
return self._data[:]

@property
def columns(self):
return self._columns.copy()
if PYTHON3:
return self._columns.copy()
else:
return self._columns[:]

@columns.setter
def columns(self, columns_list):
Expand All @@ -189,7 +198,10 @@ def columns(self, columns_list):

@property
def index(self):
return self._index.copy()
if PYTHON3:
return self._index.copy()
else:
return self._index[:]

@index.setter
def index(self, index_list):
Expand Down Expand Up @@ -726,7 +738,7 @@ def to_dict(self, index=True, ordered=False):
result.update(data_dict)
return result

def to_json(self) -> str:
def to_json(self):
"""
Returns a JSON of the entire DataFrame that can be reconstructed back with raccoon.from_json(input). Any object
that cannot be serialized will be replaced with the representation of the object using repr(). In that instance
Expand Down Expand Up @@ -903,7 +915,10 @@ def append(self, data_frame):
raise ValueError('duplicate indexes in DataFrames')

for c, column in enumerate(data_frame.columns):
self.set(indexes=data_frame.index, columns=column, values=data_frame.data[c].copy())
if PYTHON3:
self.set(indexes=data_frame.index, columns=column, values=data_frame.data[c].copy())
else:
self.set(indexes=data_frame.index, columns=column, values=data_frame.data[c][:])

def equality(self, column, indexes=None, value=None):
"""
Expand Down Expand Up @@ -1046,7 +1061,7 @@ def reset_index(self, drop=False):


# DataFrame creation functions
def from_json(json_string: str):
def from_json(json_string):
"""
Creates and return a DataFrame from a JSON of the type created by to_json

Expand Down
6 changes: 3 additions & 3 deletions tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_rename_columns():
df.rename_columns({'a2': 'a', 'bad': 'nogo'})


def test_print():
def test_show():
df = rc.DataFrame({'a': [1, 2, 3], 'b': [1.0, 2.55, 3.1], 'c': ['first', 'second', None]}, columns=['b', 'c', 'a'],
index=['row1', 'row2', 'row3'], use_blist=True)

Expand All @@ -222,8 +222,8 @@ def test_print():
actual = df.__str__()
assert actual == expected

# print() method will pass along any argument for the tabulate.tabulate function
df.print()
# show() method will pass along any argument for the tabulate.tabulate function
df.show()


def test_input_data_mutability():
Expand Down
7 changes: 5 additions & 2 deletions tests/test_initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import raccoon as rc
from blist import blist

import sys
PYTHON3 = (sys.version_info >= (3, 0))

def test_default_empty_init():
actual = rc.DataFrame()
Expand Down Expand Up @@ -140,8 +142,9 @@ def test_sorted_init():
assert df.data == [[1, 2, 3], [4, 5, 6]]

# mixed type index will bork on sorted=True
with pytest.raises(TypeError):
rc.DataFrame({'a': [2, 1, 3], 'b': [5, 4, 6]}, index=[1, 'b', 3], sorted=True)
if PYTHON3:
with pytest.raises(TypeError):
rc.DataFrame({'a': [2, 1, 3], 'b': [5, 4, 6]}, index=[1, 'b', 3], sorted=True)


def test_jagged_data():
Expand Down
8 changes: 6 additions & 2 deletions tests/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import raccoon as rc
from raccoon.utils import assert_frame_equal

import sys
PYTHON3 = (sys.version_info >= (3, 0))


def test_set_cell():
actual = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}, index=[10, 11, 12], columns=['a', 'b', 'c'],
Expand Down Expand Up @@ -73,8 +76,9 @@ def test_set_cell_sorted():
assert all([isinstance(actual.data[x], list) for x in range(len(actual.columns))])

# fails for mixed index type
with pytest.raises(TypeError):
actual.set('Z', 'e', 60)
if PYTHON3:
with pytest.raises(TypeError):
actual.set('Z', 'e', 60)


def test_set_row():
Expand Down
14 changes: 9 additions & 5 deletions tests/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from raccoon.utils import assert_frame_equal
from blist import blist

import sys
PYTHON3 = (sys.version_info >= (3, 0))


def test_sort_index():
# test on list
Expand All @@ -24,8 +27,9 @@ def test_sort_index():

# fails on mixed type columns
df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[10, 'a', 9])
with pytest.raises(TypeError):
df.sort_index()
if PYTHON3:
with pytest.raises(TypeError):
df.sort_index()


def test_sort_multi_index():
Expand All @@ -39,8 +43,9 @@ def test_sort_multi_index():

# fails on mixed type columns
df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=[(10, 'c'), 'a', (10, 'b')])
with pytest.raises(TypeError):
df.sort_index()
if PYTHON3:
with pytest.raises(TypeError):
df.sort_index()


def test_sort_column():
Expand All @@ -64,4 +69,3 @@ def test_sort_column():
assert isinstance(df.index, blist)
assert_frame_equal(df, rc.DataFrame({'a': [1, 2, 3], 'b': ['c', 'a', 'b']}, columns=['a', 'b'], index=[8, 10, 9],
use_blist=True))