|
28 | 28 | from pandas.util.testing import (assert_almost_equal,
|
29 | 29 | assert_series_equal,
|
30 | 30 | assert_frame_equal,
|
| 31 | + makeCustomDataframe as mkdf, |
31 | 32 | ensure_clean)
|
32 | 33 | from pandas.util import py3compat
|
33 | 34 | from pandas.util.compat import OrderedDict
|
@@ -4621,9 +4622,59 @@ def test_to_csv_from_csv(self):
|
4621 | 4622 | xp.columns = map(int,xp.columns)
|
4622 | 4623 | assert_frame_equal(xp,rs)
|
4623 | 4624 |
|
| 4625 | + def test_to_csv_cols_reordering(self): |
| 4626 | + # GH3454 |
| 4627 | + import pandas as pd |
| 4628 | + |
| 4629 | + def _check_df(df,cols=None): |
| 4630 | + with ensure_clean() as path: |
| 4631 | + df.to_csv(path,cols = cols,engine='python') |
| 4632 | + rs_p = pd.read_csv(path,index_col=0) |
| 4633 | + df.to_csv(path,cols = cols,chunksize=chunksize) |
| 4634 | + rs_c = pd.read_csv(path,index_col=0) |
| 4635 | + |
| 4636 | + if cols: |
| 4637 | + df = df[cols] |
| 4638 | + assert (rs_c.columns==rs_p.columns).all() |
| 4639 | + assert_frame_equal(df,rs_c,check_names=False) |
| 4640 | + |
| 4641 | + chunksize=5 |
| 4642 | + N = int(chunksize*2.5) |
| 4643 | + |
| 4644 | + df= mkdf(N, 3) |
| 4645 | + cs = df.columns |
| 4646 | + cols = [cs[2],cs[0]] |
| 4647 | + _check_df(df,cols) |
| 4648 | + |
| 4649 | + def test_to_csv_legacy_raises_on_dupe_cols(self): |
| 4650 | + df= mkdf(10, 3) |
| 4651 | + df.columns = ['a','a','b'] |
| 4652 | + with ensure_clean() as path: |
| 4653 | + self.assertRaises(NotImplementedError,df.to_csv,path,engine='python') |
| 4654 | + |
| 4655 | + def test_to_csv_new_dupe_cols(self): |
| 4656 | + import pandas as pd |
| 4657 | + def _check_df(df,cols=None): |
| 4658 | + with ensure_clean() as path: |
| 4659 | + df.to_csv(path,cols = cols,chunksize=chunksize) |
| 4660 | + rs_c = pd.read_csv(path,index_col=0) |
| 4661 | + rs_c.columns = df.columns |
| 4662 | + assert_frame_equal(df,rs_c,check_names=False) |
| 4663 | + |
| 4664 | + chunksize=5 |
| 4665 | + N = int(chunksize*2.5) |
| 4666 | + |
| 4667 | + # dupe cols |
| 4668 | + df= mkdf(N, 3) |
| 4669 | + df.columns = ['a','a','b'] |
| 4670 | + _check_df(df,None) |
| 4671 | + |
| 4672 | + # dupe cols with selection |
| 4673 | + cols = ['b','a'] |
| 4674 | + _check_df(df,cols) |
| 4675 | + |
4624 | 4676 | @slow
|
4625 | 4677 | def test_to_csv_moar(self):
|
4626 |
| - from pandas.util.testing import makeCustomDataframe as mkdf |
4627 | 4678 | path = '__tmp_to_csv_moar__'
|
4628 | 4679 |
|
4629 | 4680 | def _do_test(df,path,r_dtype=None,c_dtype=None,rnlvl=None,cnlvl=None,
|
|
0 commit comments