Skip to content

Commit 1b78d3a

Browse files
committed
Closed #78. Added renameColumn and renameRow
1 parent 380defb commit 1b78d3a

2 files changed

Lines changed: 102 additions & 27 deletions

File tree

src/DataFrame-Tests/DataFrameTest.class.st

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,36 @@ DataFrameTest >> testEquality [
10221022
self assert: (df1 = df2).
10231023
]
10241024

1025+
{ #category : #tests }
1026+
DataFrameTest >> testIndexOfColumnNamed [
1027+
| expected actual |
1028+
expected := 2.
1029+
actual := df indexOfColumnNamed: #Population.
1030+
self assert: actual equals: expected.
1031+
]
1032+
1033+
{ #category : #tests }
1034+
DataFrameTest >> testIndexOfColumnNamedNotFound [
1035+
self
1036+
should: [ df indexOfColumnNamed: #NoSuchColumn ]
1037+
raise: NotFoundError.
1038+
]
1039+
1040+
{ #category : #tests }
1041+
DataFrameTest >> testIndexOfRowNamed [
1042+
| expected actual |
1043+
expected := 2.
1044+
actual := df indexOfRowNamed: #B.
1045+
self assert: actual equals: expected.
1046+
]
1047+
1048+
{ #category : #tests }
1049+
DataFrameTest >> testIndexOfRowNamedNotFound [
1050+
self
1051+
should: [ df indexOfRowNamed: #NoSuchRow ]
1052+
raise: NotFoundError.
1053+
]
1054+
10251055
{ #category : #tests }
10261056
DataFrameTest >> testInequality [
10271057

@@ -1203,6 +1233,50 @@ DataFrameTest >> testRemoveRowNotFound [
12031233
self should: [ df removeRow: #NoSuchRow ] raise: NotFoundError.
12041234
]
12051235

1236+
{ #category : #tests }
1237+
DataFrameTest >> testRenameColumnTo [
1238+
| expected |
1239+
expected := DataFrame
1240+
withRows: #(
1241+
(Barcelona 1.609 true)
1242+
(Dubai 2.789 true)
1243+
(London 8.788 false))
1244+
rowNames: #(A B C)
1245+
columnNames: #(City Population Visited).
1246+
1247+
df renameColumn: #BeenThere to: #Visited.
1248+
self assert: df equals: expected.
1249+
]
1250+
1251+
{ #category : #tests }
1252+
DataFrameTest >> testRenameColumnToNotFound [
1253+
self
1254+
should: [ df renameColumn: #NoSuchColumn to: #Sth ]
1255+
raise: NotFoundError.
1256+
]
1257+
1258+
{ #category : #tests }
1259+
DataFrameTest >> testRenameRowTo [
1260+
| expected |
1261+
expected := DataFrame
1262+
withRows: #(
1263+
(Barcelona 1.609 true)
1264+
(Dubai 2.789 true)
1265+
(London 8.788 false))
1266+
rowNames: #(A X C)
1267+
columnNames: #(City Population BeenThere).
1268+
1269+
df renameRow: #B to: #X.
1270+
self assert: df equals: expected.
1271+
]
1272+
1273+
{ #category : #tests }
1274+
DataFrameTest >> testRenameRowToNotFound [
1275+
self
1276+
should: [ df renameRow: #NoSuchRow to: #Sth ]
1277+
raise: NotFoundError.
1278+
]
1279+
12061280
{ #category : #tests }
12071281
DataFrameTest >> testRow [
12081282

src/DataFrame/DataFrame.class.st

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ DataFrame >> firstQuartile [
662662
^ self applyToAllColumns: #firstQuartile
663663
]
664664

665-
{ #category : #queries }
665+
{ #category : #grouping }
666666
DataFrame >> group: colNameOrArray by: colName [
667667

668668
| left right |
@@ -676,7 +676,7 @@ DataFrame >> group: colNameOrArray by: colName [
676676
^ left groupBy: right.
677677
]
678678

679-
{ #category : #queries }
679+
{ #category : #grouping }
680680
DataFrame >> groupBy: colName [
681681

682682
| groupedColNames |
@@ -689,7 +689,7 @@ DataFrame >> groupBy: colName [
689689
by: (self column: colName)
690690
]
691691

692-
{ #category : #queries }
692+
{ #category : #grouping }
693693
DataFrame >> groupBy: colName aggregate: selector [
694694

695695
^ (self groupBy: colName) perform: selector.
@@ -733,30 +733,20 @@ DataFrame >> head: aNumber [
733733

734734
{ #category : #private }
735735
DataFrame >> indexOfColumnNamed: columnName [
736-
737-
| index |
738-
index := self columnNames indexOf: columnName.
739-
740-
"If a column with that name does not exist"
741-
index = 0
742-
ifTrue: [ NotFoundError signal:
736+
"Answer the index of a column with a given name or signal an exception if the column with that name was not found"
737+
^ self columnNames
738+
indexOf: columnName
739+
ifAbsent: [ NotFoundError signal:
743740
('Column ', columnName, ' was not found') ].
744-
745-
^ index
746741
]
747742

748743
{ #category : #private }
749744
DataFrame >> indexOfRowNamed: rowName [
750-
751-
| index |
752-
index := self rowNames indexOf: rowName.
753-
754-
"If a row with that name does not exist"
755-
index = 0
756-
ifTrue: [ NotFoundError signal:
745+
"Answer the index of a row with a given name or signal an exception if the row with that name was not found"
746+
^ self rowNames
747+
indexOf: rowName
748+
ifAbsent: [ NotFoundError signal:
757749
('Row ', rowName, ' was not found') ].
758-
759-
^ index
760750
]
761751

762752
{ #category : #initialization }
@@ -895,6 +885,22 @@ DataFrame >> removeRowAt: rowNumber [
895885
rowNames := rowNames copyWithoutIndex: rowNumber.
896886
]
897887

888+
{ #category : #renaming }
889+
DataFrame >> renameColumn: oldName to: newName [
890+
"Find a column with oldName and rename it to newName"
891+
| index |
892+
index := self indexOfColumnNamed: oldName.
893+
self columnNames at: index put: newName.
894+
]
895+
896+
{ #category : #renaming }
897+
DataFrame >> renameRow: oldName to: newName [
898+
"Find a row with oldName and rename it to newName"
899+
| index |
900+
index := self indexOfRowNamed: oldName.
901+
self rowNames at: index put: newName.
902+
]
903+
898904
{ #category : #accessing }
899905
DataFrame >> row: rowName [
900906

@@ -1127,12 +1133,7 @@ DataFrame >> thirdQuartile [
11271133
DataFrame >> toColumn: columnName applyElementwise: aBlock [
11281134

11291135
| index |
1130-
index := self columnNames indexOf: columnName.
1131-
1132-
"If a column with that name does not exist"
1133-
index = 0
1134-
ifTrue: [ NotFoundError signal ].
1135-
1136+
index := self indexOfColumnNamed: columnName.
11361137
self toColumnAt: index applyElementwise: aBlock.
11371138
]
11381139

0 commit comments

Comments
 (0)