Skip to content

Commit 3a4fb2a

Browse files
committed
DataFrames can now import vertically organised data sets.
To use set the third parameter to true.
1 parent a6399f4 commit 3a4fb2a

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

src/DataFrame.class.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ final class DataFrame implements \ArrayAccess, \Countable, \IteratorAggregate
4747
protected $showGenericIndexes = true;
4848

4949
/**
50-
* Static equivilent of `new DataFrame()`.
50+
* Static equivilent of `new DataFrame`.
5151
*/
52-
static public function make(array $data, array $headers = null)
52+
static public function make(array $data, array $headers = null, bool $isVerticalDataSet = false)
5353
{
54-
return new DataFrame($data, $headers);
54+
return new DataFrame($data, $headers, $isVerticalDataSet);
5555
}
5656

5757
static public function empty_frames(): bool
@@ -115,14 +115,35 @@ public function __toString(): string
115115
// ------- Main class methods
116116

117117
/**
118-
* Construct a new dataframe with the provided data. You may optionally provided the
118+
* Construct a new dataframe with the provided data. You may optionally provide the
119119
* set of column headers in the second parameter. If you choose to do this then they
120120
* should match the keys in the array.
121121
*
122-
* NOTE: The provided array must have at least one element/row and must also be
123-
* 2-dimensional in structure.
122+
* -- parameters:
123+
* @param $data The array of data. Unless `$isVerticalDataSet` is TRUE, the array should be an array of rows. Each row is an associative array where the keys correpsond to the headers of each column.
124+
* @param $headers An optional custom set of column headers.
125+
* @param $isVerticalDataSet When set to TRUE the $data array is interpretted as a veritical series of columns instead of rows. Defaults to FALSE.
126+
*
127+
* Standard data array format:
128+
*
129+
* ``` php
130+
* [
131+
* ['col 1' => value, 'col 2' => value ... 'col Nth' => value],
132+
* ['col 1' => value, 'col 2' => value ... 'col Nth' => value]
133+
* ]
134+
* ```
135+
*
136+
* Data Array format for vertical data sets:
137+
*
138+
* ``` php
139+
* [
140+
* 'col 1' => [... values],
141+
* 'col 2' => [... values],
142+
* 'col Nth' => [... values]
143+
* ]
144+
* ```
124145
*/
125-
public function __construct(?array $data = null, ?array $headers = null)
146+
public function __construct(?array $data = null, ?array $headers = null, bool $isVerticalDataSet = false)
126147
{
127148
if ($data === null) {
128149
if (! self::empty_frames())
@@ -133,6 +154,17 @@ public function __construct(?array $data = null, ?array $headers = null)
133154
else if (! self::empty_frames() and count($data) == 0 and ($headers === null or count($headers) == 0))
134155
throw new \LengthException('A DataFrame needs at least one row of data.');
135156

157+
if ($isVerticalDataSet)
158+
{
159+
# data has been supplied as a keyed set of columns, translate to rows.
160+
$headers = array_keys($data);
161+
$rows = [];
162+
foreach (arrays::zip(...array_values($data)) as $values) {
163+
$rows[] = array_combine($headers, $values);
164+
}
165+
$data = &$rows;
166+
}
167+
136168
$this->data = $data;
137169
$this->headers = $headers;
138170
if (! $this->headers and count($data) > 0) {

src/global_functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ function vector(...$items)
4848
/**
4949
* Create a new DataFrame with the supplied rows & columns.
5050
*
51-
* @see DataFrame::__construct() for a proper description.
51+
* @see [DataFrame::__construct()](DataFrame.md#__construct) for a proper description.
5252
*/
53-
function dataframe(?array $data = null, array $headers = null)
53+
function dataframe(?array $data = null, array $headers = null, bool $isVerticalDataSet = false)
5454
{
55-
return new \sqonk\phext\datakit\DataFrame($data, $headers);
55+
return new \sqonk\phext\datakit\DataFrame($data, $headers, $isVerticalDataSet);
5656
}

tests/DataFrameTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ private function _loadFrame()
3434
return [$df, $data];
3535
}
3636

37+
public function testCreateFromVerticalDataset()
38+
{
39+
$data = [
40+
'col 1' => range(1, 3),
41+
'col 2' => ['a', 'b', 'c']
42+
];
43+
$exp = [
44+
['col 1' => 1, 'col 2' => 'a'],
45+
['col 1' => 2, 'col 2' => 'b'],
46+
['col 1' => 3, 'col 2' => 'c']
47+
];
48+
49+
$df = dataframe($data, null, true);
50+
51+
$this->assertSame($exp, $df->data());
52+
}
53+
3754
public function testRows()
3855
{
3956
[$df, $data] = $this->_loadFrame();

0 commit comments

Comments
 (0)