scirs2-python 0.4.3

Python bindings for SciRS2 - A comprehensive scientific computing library in Rust (SciPy alternative)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
"""Tests for scirs2 pandas compatibility module."""

import numpy as np
import pytest
import scirs2


class TestDataFrameCreation:
    """Test DataFrame-like creation."""

    def test_create_dataframe_from_dict(self):
        """Test creating DataFrame from dictionary."""
        data = {"col1": [1, 2, 3], "col2": [4.0, 5.0, 6.0], "col3": ["a", "b", "c"]}

        df = scirs2.DataFrame(data)

        assert df.shape() == (3, 3)
        assert "col1" in df.columns()

    def test_create_dataframe_from_array(self):
        """Test creating DataFrame from numpy array."""
        data = np.array([[1, 2, 3], [4, 5, 6]])

        df = scirs2.DataFrame(data, columns=["A", "B", "C"])

        assert df.shape() == (2, 3)
        assert df.columns() == ["A", "B", "C"]

    def test_create_dataframe_from_lists(self):
        """Test creating DataFrame from list of lists."""
        data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

        df = scirs2.DataFrame(data, columns=["X", "Y", "Z"])

        assert df.shape() == (3, 3)


class TestDataFrameIndexing:
    """Test DataFrame indexing operations."""

    def test_select_column(self):
        """Test selecting a single column."""
        data = {"col1": [1, 2, 3], "col2": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        col = df["col1"]

        assert len(col) == 3
        assert np.allclose(col, [1, 2, 3])

    def test_select_multiple_columns(self):
        """Test selecting multiple columns."""
        data = {"col1": [1, 2, 3], "col2": [4, 5, 6], "col3": [7, 8, 9]}
        df = scirs2.DataFrame(data)

        subset = df[["col1", "col3"]]

        assert subset.shape() == (3, 2)

    def test_loc_indexing(self):
        """Test label-based indexing."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        row = df.loc[0]

        assert len(row) == 2

    def test_iloc_indexing(self):
        """Test integer-based indexing."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        row = df.iloc[1]

        assert len(row) == 2

    def test_boolean_indexing(self):
        """Test boolean indexing."""
        data = {"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]}
        df = scirs2.DataFrame(data)

        mask = df["A"] > 2
        filtered = df[mask]

        assert filtered.shape()[0] < 4


class TestDataFrameOperations:
    """Test DataFrame operations."""

    def test_add_column(self):
        """Test adding a new column."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        df["C"] = [7, 8, 9]

        assert "C" in df.columns()
        assert df.shape() == (3, 3)

    def test_drop_column(self):
        """Test dropping a column."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}
        df = scirs2.DataFrame(data)

        df_dropped = df.drop(["B"])

        assert "B" not in df_dropped.columns()
        assert df_dropped.shape() == (3, 2)

    def test_drop_row(self):
        """Test dropping rows."""
        data = {"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]}
        df = scirs2.DataFrame(data)

        df_dropped = df.drop_rows([1, 3])

        assert df_dropped.shape()[0] == 2

    def test_rename_columns(self):
        """Test renaming columns."""
        data = {"old1": [1, 2, 3], "old2": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        df_renamed = df.rename(columns={"old1": "new1", "old2": "new2"})

        assert "new1" in df_renamed.columns()
        assert "old1" not in df_renamed.columns()


class TestAggregationOperations:
    """Test aggregation operations."""

    def test_sum(self):
        """Test sum aggregation."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        result = df.sum()

        assert result["A"] == 6
        assert result["B"] == 15

    def test_mean(self):
        """Test mean aggregation."""
        data = {"A": [1, 2, 3, 4], "B": [2, 4, 6, 8]}
        df = scirs2.DataFrame(data)

        result = df.mean()

        assert result["A"] == 2.5
        assert result["B"] == 5.0

    def test_std(self):
        """Test standard deviation."""
        data = {"A": [1, 2, 3, 4, 5]}
        df = scirs2.DataFrame(data)

        result = df.std()

        assert result["A"] > 0

    def test_min_max(self):
        """Test min and max."""
        data = {"A": [1, 5, 3, 2, 4], "B": [10, 50, 30, 20, 40]}
        df = scirs2.DataFrame(data)

        min_result = df.min()
        max_result = df.max()

        assert min_result["A"] == 1
        assert max_result["A"] == 5


class TestGroupByOperations:
    """Test groupby operations."""

    def test_groupby_single_column(self):
        """Test grouping by single column."""
        data = {
            "category": ["A", "B", "A", "B", "A"],
            "value": [1, 2, 3, 4, 5]
        }
        df = scirs2.DataFrame(data)

        grouped = df.groupby("category")
        result = grouped.sum()

        assert "A" in result or len(result) > 0

    def test_groupby_aggregation(self):
        """Test groupby with aggregation."""
        data = {
            "group": [1, 1, 2, 2, 3],
            "value": [10, 20, 30, 40, 50]
        }
        df = scirs2.DataFrame(data)

        result = df.groupby("group").mean()

        assert len(result) >= 1

    def test_groupby_multiple_aggregations(self):
        """Test groupby with multiple aggregations."""
        data = {
            "category": ["A", "A", "B", "B"],
            "value1": [1, 2, 3, 4],
            "value2": [5, 6, 7, 8]
        }
        df = scirs2.DataFrame(data)

        result = df.groupby("category").agg(["sum", "mean"])

        assert len(result) > 0


class TestMergeAndJoin:
    """Test merge and join operations."""

    def test_merge_inner(self):
        """Test inner merge."""
        df1 = scirs2.DataFrame({"key": [1, 2, 3], "value1": [10, 20, 30]})
        df2 = scirs2.DataFrame({"key": [2, 3, 4], "value2": [200, 300, 400]})

        merged = df1.merge(df2, on="key", how="inner")

        # Should have keys 2 and 3
        assert merged.shape()[0] == 2

    def test_merge_left(self):
        """Test left merge."""
        df1 = scirs2.DataFrame({"key": [1, 2, 3], "value1": [10, 20, 30]})
        df2 = scirs2.DataFrame({"key": [2, 3, 4], "value2": [200, 300, 400]})

        merged = df1.merge(df2, on="key", how="left")

        # Should have all keys from df1
        assert merged.shape()[0] == 3

    def test_merge_outer(self):
        """Test outer merge."""
        df1 = scirs2.DataFrame({"key": [1, 2], "value1": [10, 20]})
        df2 = scirs2.DataFrame({"key": [2, 3], "value2": [200, 300]})

        merged = df1.merge(df2, on="key", how="outer")

        # Should have keys 1, 2, 3
        assert merged.shape()[0] == 3

    def test_concat_vertical(self):
        """Test vertical concatenation."""
        df1 = scirs2.DataFrame({"A": [1, 2], "B": [3, 4]})
        df2 = scirs2.DataFrame({"A": [5, 6], "B": [7, 8]})

        concatenated = scirs2.concat([df1, df2], axis=0)

        assert concatenated.shape() == (4, 2)

    def test_concat_horizontal(self):
        """Test horizontal concatenation."""
        df1 = scirs2.DataFrame({"A": [1, 2, 3]})
        df2 = scirs2.DataFrame({"B": [4, 5, 6]})

        concatenated = scirs2.concat([df1, df2], axis=1)

        assert concatenated.shape() == (3, 2)


class TestDataCleaning:
    """Test data cleaning operations."""

    def test_drop_na(self):
        """Test dropping missing values."""
        data = {"A": [1.0, np.nan, 3.0], "B": [4.0, 5.0, np.nan]}
        df = scirs2.DataFrame(data)

        cleaned = df.dropna()

        assert cleaned.shape()[0] < 3

    def test_fill_na(self):
        """Test filling missing values."""
        data = {"A": [1.0, np.nan, 3.0], "B": [4.0, 5.0, np.nan]}
        df = scirs2.DataFrame(data)

        filled = df.fillna(0.0)

        assert not np.isnan(filled["A"]).any()

    def test_replace(self):
        """Test replacing values."""
        data = {"A": [1, 2, 3, 1, 2, 3]}
        df = scirs2.DataFrame(data)

        replaced = df.replace(1, 99)

        assert 1 not in replaced["A"]
        assert 99 in replaced["A"]

    def test_drop_duplicates(self):
        """Test dropping duplicate rows."""
        data = {"A": [1, 2, 2, 3], "B": [4, 5, 5, 6]}
        df = scirs2.DataFrame(data)

        unique = df.drop_duplicates()

        assert unique.shape()[0] < 4


class TestSortingOperations:
    """Test sorting operations."""

    def test_sort_by_column(self):
        """Test sorting by column."""
        data = {"A": [3, 1, 2], "B": [6, 4, 5]}
        df = scirs2.DataFrame(data)

        sorted_df = df.sort_values(by="A")

        assert sorted_df["A"][0] == 1

    def test_sort_descending(self):
        """Test descending sort."""
        data = {"A": [1, 3, 2]}
        df = scirs2.DataFrame(data)

        sorted_df = df.sort_values(by="A", ascending=False)

        assert sorted_df["A"][0] == 3

    def test_sort_multiple_columns(self):
        """Test sorting by multiple columns."""
        data = {"A": [1, 1, 2, 2], "B": [4, 3, 2, 1]}
        df = scirs2.DataFrame(data)

        sorted_df = df.sort_values(by=["A", "B"])

        assert sorted_df.shape() == (4, 2)


class TestApplyOperations:
    """Test apply operations."""

    def test_apply_function_to_column(self):
        """Test applying function to column."""
        data = {"A": [1, 2, 3, 4]}
        df = scirs2.DataFrame(data)

        df["B"] = df["A"].apply(lambda x: x ** 2)

        assert np.allclose(df["B"], [1, 4, 9, 16])

    def test_apply_function_to_row(self):
        """Test applying function to row."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        result = df.apply(lambda row: row["A"] + row["B"], axis=1)

        assert np.allclose(result, [5, 7, 9])

    def test_map_values(self):
        """Test mapping values."""
        data = {"category": ["A", "B", "A", "C"]}
        df = scirs2.DataFrame(data)

        mapping = {"A": 1, "B": 2, "C": 3}
        df["numeric"] = df["category"].map(mapping)

        assert 1 in df["numeric"]


class TestStatisticalOperations:
    """Test statistical operations."""

    def test_describe(self):
        """Test describe statistics."""
        data = {"A": [1, 2, 3, 4, 5], "B": [10, 20, 30, 40, 50]}
        df = scirs2.DataFrame(data)

        stats = df.describe()

        assert "mean" in stats or "count" in stats

    def test_correlation(self):
        """Test correlation calculation."""
        data = {"A": [1, 2, 3, 4, 5], "B": [2, 4, 6, 8, 10]}
        df = scirs2.DataFrame(data)

        corr = df.corr()

        # A and B are perfectly correlated
        assert corr["A"]["B"] > 0.99 or corr.shape[0] > 0

    def test_covariance(self):
        """Test covariance calculation."""
        data = {"A": [1, 2, 3, 4], "B": [2, 4, 6, 8]}
        df = scirs2.DataFrame(data)

        cov = df.cov()

        assert cov.shape[0] > 0


class TestPivotOperations:
    """Test pivot operations."""

    def test_pivot_table(self):
        """Test pivot table creation."""
        data = {
            "category": ["A", "A", "B", "B"],
            "subcategory": ["X", "Y", "X", "Y"],
            "value": [1, 2, 3, 4]
        }
        df = scirs2.DataFrame(data)

        pivot = df.pivot_table(
            values="value",
            index="category",
            columns="subcategory"
        )

        assert pivot.shape[0] > 0

    def test_melt(self):
        """Test melting DataFrame."""
        data = {"A": [1, 2], "B": [3, 4], "C": [5, 6]}
        df = scirs2.DataFrame(data)

        melted = df.melt()

        # Melted should have more rows
        assert melted.shape()[0] > df.shape()[0]


class TestIOCompatibility:
    """Test I/O compatibility."""

    def test_to_numpy(self):
        """Test converting to numpy array."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        array = df.to_numpy()

        assert array.shape == (3, 2)

    def test_to_dict(self):
        """Test converting to dictionary."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        result_dict = df.to_dict()

        assert "A" in result_dict
        assert "B" in result_dict

    def test_to_records(self):
        """Test converting to record array."""
        data = {"A": [1, 2, 3], "B": [4, 5, 6]}
        df = scirs2.DataFrame(data)

        records = df.to_records()

        assert len(records) == 3


class TestEdgeCases:
    """Test edge cases and error handling."""

    def test_empty_dataframe(self):
        """Test empty DataFrame."""
        df = scirs2.DataFrame()

        assert df.shape() == (0, 0)

    def test_single_row(self):
        """Test DataFrame with single row."""
        data = {"A": [1], "B": [2]}
        df = scirs2.DataFrame(data)

        assert df.shape() == (1, 2)

    def test_single_column(self):
        """Test DataFrame with single column."""
        data = {"A": [1, 2, 3, 4, 5]}
        df = scirs2.DataFrame(data)

        assert df.shape() == (5, 1)

    def test_mixed_types(self):
        """Test DataFrame with mixed types."""
        data = {"int": [1, 2, 3], "float": [1.1, 2.2, 3.3], "str": ["a", "b", "c"]}
        df = scirs2.DataFrame(data)

        assert df.shape() == (3, 3)


if __name__ == "__main__":
    pytest.main([__file__, "-v"])