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

Adding Head method for DataFrame #195

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions dataframe/dataframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2357,3 +2357,8 @@ func (df DataFrame) Describe() DataFrame {
ddf := New(ss...)
return ddf
}

// Head returns the first 5 rows of the DataFrame
func (df DataFrame) Head() DataFrame {
return df.Subset([]int{0, 1, 2, 3, 4})
}
42 changes: 42 additions & 0 deletions dataframe/dataframe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3000,3 +3000,45 @@ func TestGroups_GetGroups(t *testing.T) {
t.Fatalf("Expected to get 3 groups, got %d", len(groupNames))
}
}

func TestDataFrame_Head(t *testing.T) {
a := New(
series.New([]string{"a", "b", "c", "d", "e", "f", "g"}, series.String, "COL.1"),
series.New([]int{1, 2, 3, 4, 5, 6, 7}, series.Int, "COL.2"),
series.New([]float64{2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0}, series.Float, "COL.3"),
)
table := []struct {
expDf DataFrame
}{
{
New(
series.New([]string{"a", "b", "c", "d", "e"}, series.String, "COL.1"),
series.New([]int{1, 2, 3, 4, 5}, series.Int, "COL.2"),
series.New([]float64{2.0, 4.0, 8.0, 16.0, 32.0}, series.Float, "COL.3"),
),
},
}

for i, tc := range table {
b := a.Head()

if b.Err != nil {
t.Errorf("Test: %d\nError:%v", i, b.Err)
}
//if err := checkAddrDf(a, b); err != nil {
//t.Error(err)
//}
// Check that the types are the same between both DataFrames
if !reflect.DeepEqual(tc.expDf.Types(), b.Types()) {
t.Errorf("Test: %d\nDifferent types:\nA:%v\nB:%v", i, tc.expDf.Types(), b.Types())
}
// Check that the colnames are the same between both DataFrames
if !reflect.DeepEqual(tc.expDf.Names(), b.Names()) {
t.Errorf("Test: %d\nDifferent colnames:\nA:%v\nB:%v", i, tc.expDf.Names(), b.Names())
}
// Check that the values are the same between both DataFrames
if !reflect.DeepEqual(tc.expDf.Records(), b.Records()) {
t.Errorf("Test: %d\nDifferent values:\nA:%v\nB:%v", i, tc.expDf.Records(), b.Records())
}
}
}