Skip to content

Commit

Permalink
Fefactoring: append()
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-boudry committed Oct 6, 2023
1 parent 43daea5 commit b8cd3ca
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
21 changes: 4 additions & 17 deletions src/DataFrameCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,25 +326,12 @@ public function removeColumn(string $columnName): void
/**
* Allows user to "array_merge" two DataFrames so that the rows of one are appended to the rows of another.
*
* @param $other
* @return $this
*/
public function append(DataFrame $other): self
public function append(DataFrame $df): self
{
if (\count($other) <= 0) {
return $this;
}

$columns = $this->columns;

// TODO: Strange bug occurs when $other is used as an Iterator here, have to use toArray() to bypass
foreach ($other->toArray() as $row) {
$newRow = [];
foreach ($columns as $column) {
$newRow[$column] = $row[$column];
}

$this->data[] = $newRow;
foreach ($df as $dfEntry) {
$this->addEntry($dfEntry);
}

return $this;
Expand All @@ -359,7 +346,7 @@ public function append(DataFrame $other): self
*/
public function preg_replace($pattern, $replacement): self
{
return $this->apply(static function ($row) use ($pattern, $replacement) {
return $this->apply(static function (array $row) use ($pattern, $replacement) {
return preg_replace($pattern, $replacement, $row);
});
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Features/Core/CoreDataFrameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@

test('append', function (): void {
$df1 = $this->df;
$df2 = $this->df;
$df2 = clone $this->df;

// Test that appending an array with less than count of 1 will simply return the original DataFrame
expect($df1->append(DataFrame::fromArray([])))->toBe($df1);
Expand Down

0 comments on commit b8cd3ca

Please sign in to comment.