Skip to content

Commit 79fa5e2

Browse files
author
y-p
committed
TST: to_csv respects cols= reordering GH3454/7
1 parent 67ad556 commit 79fa5e2

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

pandas/tests/test_frame.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from pandas.util.testing import (assert_almost_equal,
2929
assert_series_equal,
3030
assert_frame_equal,
31+
makeCustomDataframe as mkdf,
3132
ensure_clean)
3233
from pandas.util import py3compat
3334
from pandas.util.compat import OrderedDict
@@ -4621,9 +4622,59 @@ def test_to_csv_from_csv(self):
46214622
xp.columns = map(int,xp.columns)
46224623
assert_frame_equal(xp,rs)
46234624

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+
46244676
@slow
46254677
def test_to_csv_moar(self):
4626-
from pandas.util.testing import makeCustomDataframe as mkdf
46274678
path = '__tmp_to_csv_moar__'
46284679

46294680
def _do_test(df,path,r_dtype=None,c_dtype=None,rnlvl=None,cnlvl=None,

0 commit comments

Comments
 (0)