This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
@brief test log(time=1s) | ||
You should indicate a time in seconds. The program ``run_unittests.py`` | ||
will sort all test files by increasing time and run them. | ||
""" | ||
|
||
|
||
import sys, os, unittest, pandas | ||
|
||
|
||
try : | ||
import src | ||
import pyquickhelper | ||
except ImportError : | ||
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", ".."))) | ||
if path not in sys.path : sys.path.append (path) | ||
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", "..", "..", "pyquickhelper", "src"))) | ||
if path not in sys.path : sys.path.append (path) | ||
import src | ||
import pyquickhelper | ||
|
||
from pyquickhelper import fLOG | ||
from src.ensae_teaching_cs import dfs2excel | ||
|
||
|
||
class TestSessionPandas (unittest.TestCase): | ||
|
||
def test_excel(self) : | ||
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__") | ||
|
||
df1 = pandas.DataFrame( [ {"name":"xavier", "school":"ENSAE"}, | ||
{"name":"antoine", "school":"ENSAE"} ] ) | ||
|
||
df2 = pandas.DataFrame( [ {"name":"xavier", "company":"Microsoft"}, | ||
{"name":"antoine", "company":"Alephd"} ] ) | ||
|
||
ef = os.path.abspath( os.path.join( os.path.dirname(__file__), "out_example.xlsx")) | ||
if os.path.exists(ef) : os.remove(ef) | ||
dfs2excel ( {"ecole":df1, "boite":df2 }, ef) | ||
assert os.path.exists(ef) | ||
|
||
if __name__ == "__main__" : | ||
unittest.main () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
@file | ||
@brief quelques fonctions à propos de la première séance | ||
""" | ||
|
||
import pandas | ||
|
||
def dfs2excel(dfs:dict, excel_file:str): | ||
""" | ||
Aggrège plusieurs DataFrame dans un seul fichiers excel | ||
@param dfs dictionnaire ``{ feuille: dataframe }`` | ||
@param excel_file nom du fichier Excel | ||
@return ExcelWriter | ||
@example(Enregistrer plusieurs DataFrame dans un seul fichier Excel ?) | ||
Le code suivant enregistre deux DataFrame dans un seul fichier Excel. | ||
@code | ||
import pandas | ||
writer = pandas.ExcelWriter('example.xlsx') | ||
df1.to_excel(writer, 'Data 0') | ||
df2.to_excel(writer, 'Data 1') | ||
write.save() | ||
@endcode | ||
Ou en utilisant cette fonction : | ||
@code | ||
@endcode | ||
dfs2excel( { 'Data 0':df1, 'Data 1':df2 }, "example.xlsx" ) | ||
@endexample | ||
""" | ||
writer = pandas.ExcelWriter(excel_file) | ||
for k,df in dfs.items(): | ||
df.to_excel(writer, k) | ||
writer.save() | ||
return writer | ||
|
||
|