Skip to content

Commit 315b88c

Browse files
changhiskhanwesm
authored andcommitted
Issue #194 and #92. I ran test_frame and all passed
1 parent 3241692 commit 315b88c

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

pandas/core/frame.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ def duplicated(self, col_or_columns=None, take_last=False):
14731473
#----------------------------------------------------------------------
14741474
# Sorting
14751475

1476-
def sort(self, column=None, axis=0, ascending=True):
1476+
def sort(self, columns=None, axis=0, ascending=True):
14771477
"""
14781478
Sort DataFrame either by labels (along either axis) or by the values in
14791479
a column
@@ -1492,9 +1492,9 @@ def sort(self, column=None, axis=0, ascending=True):
14921492
sorted : DataFrame
14931493
"""
14941494
by = None
1495-
if column:
1495+
if columns:
14961496
assert(axis == 0)
1497-
by = self[column].values
1497+
by = self[columns]
14981498
return self.sort_index(by=by, axis=axis, ascending=ascending)
14991499

15001500
def sort_index(self, axis=0, by=None, ascending=True):
@@ -1507,7 +1507,7 @@ def sort_index(self, axis=0, by=None, ascending=True):
15071507
axis : {0, 1}
15081508
Sort index/rows versus columns
15091509
by : object
1510-
Column name in frame
1510+
Columns in frame
15111511
ascending : boolean, default True
15121512
Sort ascending vs. descending
15131513
@@ -1516,17 +1516,24 @@ def sort_index(self, axis=0, by=None, ascending=True):
15161516
sorted : DataFrame
15171517
"""
15181518
labels = self._get_axis(axis)
1519-
1519+
order_list = None
15201520
if by is not None:
1521-
try:
1521+
try:
15221522
if by in self.columns:
15231523
assert(axis == 0)
1524-
by = self[by].values
1524+
by = self[by]
15251525
except Exception:
15261526
pass
15271527

15281528
assert(len(by) == len(labels))
1529-
sort_index = Series(by, index=labels).order().index
1529+
1530+
if isinstance(by, Series):
1531+
by = by.values
1532+
sort_index = Series(by, index=labels).order().index
1533+
elif isinstance(by, DataFrame):
1534+
type_list = [(col_name, by[col_name].dtype) for col_name in by.columns]
1535+
sort_arr = np.array([tuple(r) for r in by.values], dtype=type_list)
1536+
sort_index = labels.take(sort_arr.argsort(order=by.columns))
15301537
else:
15311538
sort_index = labels.take(labels.argsort())
15321539

@@ -2499,6 +2506,20 @@ def corr(self):
24992506

25002507
return self._constructor(correl, index=cols, columns=cols)
25012508

2509+
def cov(self):
2510+
"""
2511+
Compute pairwise covariance of columns, excluding NA/null values
2512+
2513+
Returns
2514+
-------
2515+
y : DataFrame
2516+
"""
2517+
cols = self.columns
2518+
mat = self.as_matrix(cols).T
2519+
baseCov = np.cov(mat)
2520+
2521+
return self._constructor(baseCov, index=cols, columns=cols)
2522+
25022523
def corrwith(self, other, axis=0, drop=False):
25032524
"""
25042525
Compute pairwise correlation between rows or columns of two DataFrame

0 commit comments

Comments
 (0)