Skip to content

Commit f915dd0

Browse files
bites 252
1 parent f6ade67 commit f915dd0

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
/16/README.md
3434
/246/README.md
3535
/251/README.md
36+
/252/README.md

252/series.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import numpy as np
2+
import pandas as pd
3+
4+
5+
def return_at_index(ser: pd.Series, idx: int) -> object:
6+
"""Return the Object at the given index of the Series
7+
If you want to be extra careful catch and raise an error if
8+
the index does not exist.
9+
"""
10+
try:
11+
return ser.get(idx)
12+
except KeyError:
13+
raise KeyError
14+
15+
16+
def get_slice(ser: pd.Series, start: int, end: int) -> pd.core.series.Series:
17+
"""Return the slice of the given Series in the range between
18+
start and end.
19+
"""
20+
return ser[start:end]
21+
22+
23+
def get_slice_inclusive(ser: pd.Series,
24+
start: int, end: int) -> pd.core.series.Series:
25+
"""Return the slice of the given Series in the range between
26+
start and end inclusive.
27+
"""
28+
return ser.loc[start:end]
29+
30+
31+
def return_head(ser: pd.Series, num: int) -> pd.core.series.Series:
32+
"""Return the first num elements of the given Series.
33+
"""
34+
return ser.head(num)
35+
36+
37+
def return_tail(ser: pd.Series, num: int) -> pd.core.series.Series:
38+
"""Return the last num elements of the given Series.
39+
"""
40+
return ser.tail(num)
41+
42+
43+
def get_index(ser: pd.Series) -> pd.core.indexes.base.Index:
44+
"""Return all indexes of the given Series.
45+
"""
46+
return ser.index
47+
48+
49+
def get_values(ser: pd.Series) -> np.ndarray:
50+
"""Return all the values of the given Series.
51+
"""
52+
return ser.values
53+
54+
55+
def get_every_second_indexes(ser: pd.Series,
56+
even_index=True) -> pd.core.series.Series:
57+
"""Return all rows where the index is either even or odd.
58+
If even_index is True return every index where idx % 2 == 0
59+
If even_index is False return every index where idx % 2 != 0
60+
Assume default indexing i.e. 0 -> n
61+
"""
62+
data = []
63+
if even_index:
64+
for key, val in ser.items():
65+
if key % 2 == 0:
66+
data.append(val)
67+
newser = pd.Series(data=data, index=[x for x in range(len(data)*2) if x % 2 == 0])
68+
else:
69+
for key, val in ser.items():
70+
if key % 2 != 0:
71+
data.append(val)
72+
newser = pd.Series(data=data, index=[x for x in range(len(data)*2) if x % 2 != 0])
73+
return newser

252/test_series.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import inspect
2+
import string
3+
4+
import pytest
5+
import numpy as np
6+
import pandas as pd
7+
8+
import series as se
9+
10+
11+
@pytest.fixture()
12+
def float_series():
13+
"""Returns a pandas Series containing floats"""
14+
return pd.Series([float(n) / 1000 for n in range(0, 1001)])
15+
16+
17+
@pytest.fixture()
18+
def alpha_series():
19+
"""Returns a pandas Series containing floats"""
20+
dictionary = dict(zip(string.ascii_lowercase, range(1, 27)))
21+
return pd.Series(dictionary)
22+
23+
24+
@pytest.mark.parametrize("arg, expected", [
25+
(0, 0.000), (500, 0.500), (1000, 1.000)
26+
])
27+
def test_return_at_index(float_series, arg, expected):
28+
assert se.return_at_index(float_series, arg) == expected
29+
30+
31+
def test_return_at_index_raise_exception(float_series):
32+
with pytest.raises(KeyError):
33+
float_series[1111]
34+
35+
36+
def test_get_slice(float_series):
37+
slce = se.get_slice(float_series, 20, 25)
38+
assert isinstance(slce, pd.core.series.Series)
39+
assert len(slce) == 5
40+
assert slce[24] == 0.024
41+
42+
43+
def test_get_slice_inclusive(float_series):
44+
slce = se.get_slice_inclusive(float_series, 20, 25)
45+
assert isinstance(slce, pd.core.series.Series)
46+
assert len(slce) == 6
47+
assert slce[25] == 0.025
48+
49+
50+
@pytest.mark.parametrize("arg, expected", [
51+
(0, 0.000), (5, 0.005), (9, 0.009)
52+
])
53+
def test_return_head(float_series, arg, expected):
54+
assert se.return_head(float_series, 10)[arg] == expected
55+
assert ".head" in inspect.getsource(se.return_head)
56+
57+
58+
@pytest.mark.parametrize("arg, expected", [
59+
(991, 0.991), (995, 0.995), (1000, 1.000)
60+
])
61+
def test_return_tail(float_series, arg, expected):
62+
assert se.return_tail(float_series, 10)[arg] == expected
63+
assert ".tail" in inspect.getsource(se.return_tail)
64+
65+
66+
def test_get_index(alpha_series):
67+
idx = se.get_index(alpha_series)
68+
assert isinstance(idx, pd.core.indexes.base.Index)
69+
assert len(idx) == 26
70+
assert all(c in string.ascii_lowercase for c in idx.values)
71+
assert ".index" in inspect.getsource(se.get_index)
72+
73+
74+
def test_get_values(alpha_series):
75+
vals = se.get_values(alpha_series)
76+
assert isinstance(vals, np.ndarray)
77+
assert len(vals) == 26
78+
assert all(c in range(1, 27) for c in vals)
79+
80+
81+
def test_all_even_indexes_returned(float_series):
82+
ser = se.get_every_second_indexes(float_series, True)
83+
assert all(n % 2 == 0 for n in ser.index)
84+
assert round(sum(ser), 1) == 250.5
85+
86+
87+
def test_all_odd_indexes_returned(float_series):
88+
ser = se.get_every_second_indexes(float_series, False)
89+
assert all(n % 2 == 1 for n in ser.index)
90+
assert round(sum(ser), 1) == 250.0
91+

0 commit comments

Comments
 (0)