Skip to content

Commit feb78c4

Browse files
committed
DEPR: Deprecate from_csv in favor of read_csv
Closes gh-4191.
1 parent 7db7f82 commit feb78c4

File tree

4 files changed

+187
-100
lines changed

4 files changed

+187
-100
lines changed

pandas/core/frame.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,
12911291
encoding=None, tupleize_cols=False,
12921292
infer_datetime_format=False):
12931293
"""
1294-
Read CSV file (DISCOURAGED, please use :func:`pandas.read_csv`
1294+
Read CSV file (DEPRECATED, please use :func:`pandas.read_csv`
12951295
instead).
12961296
12971297
It is preferable to use the more powerful :func:`pandas.read_csv`
@@ -1339,6 +1339,13 @@ def from_csv(cls, path, header=0, sep=',', index_col=0, parse_dates=True,
13391339
y : DataFrame
13401340
13411341
"""
1342+
1343+
warnings.warn("from_csv is deprecated. Please use read_csv(...) "
1344+
"instead. Note that some of the default arguments are "
1345+
"different, so please refer to the documentation "
1346+
"for from_csv when changing your function calls",
1347+
FutureWarning, stacklevel=2)
1348+
13421349
from pandas.io.parsers import read_table
13431350
return read_table(path, header=header, sep=sep,
13441351
parse_dates=parse_dates, index_col=index_col,

pandas/core/series.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2688,7 +2688,7 @@ def between(self, left, right, inclusive=True):
26882688
def from_csv(cls, path, sep=',', parse_dates=True, header=None,
26892689
index_col=0, encoding=None, infer_datetime_format=False):
26902690
"""
2691-
Read CSV file (DISCOURAGED, please use :func:`pandas.read_csv`
2691+
Read CSV file (DEPRECATED, please use :func:`pandas.read_csv`
26922692
instead).
26932693
26942694
It is preferable to use the more powerful :func:`pandas.read_csv`
@@ -2736,6 +2736,9 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None,
27362736
-------
27372737
y : Series
27382738
"""
2739+
2740+
# We're calling `DataFrame.from_csv` in the implementation,
2741+
# which will propagate a warning regarding `from_csv` deprecation.
27392742
from pandas.core.frame import DataFrame
27402743
df = DataFrame.from_csv(path, header=header, index_col=index_col,
27412744
sep=sep, parse_dates=parse_dates,

pandas/tests/frame/test_to_csv.py

+120-58
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,36 @@ def test_to_csv_from_csv1(self):
4343

4444
# test roundtrip
4545
self.tsframe.to_csv(path)
46-
recons = DataFrame.from_csv(path)
4746

48-
assert_frame_equal(self.tsframe, recons)
47+
with tm.assert_produces_warning(FutureWarning,
48+
check_stacklevel=False):
49+
recons = DataFrame.from_csv(path)
50+
assert_frame_equal(self.tsframe, recons)
4951

5052
self.tsframe.to_csv(path, index_label='index')
51-
recons = DataFrame.from_csv(path, index_col=None)
52-
assert(len(recons.columns) == len(self.tsframe.columns) + 1)
53+
54+
with tm.assert_produces_warning(FutureWarning,
55+
check_stacklevel=False):
56+
recons = DataFrame.from_csv(path, index_col=None)
57+
assert(len(recons.columns) == len(self.tsframe.columns) + 1)
5358

5459
# no index
5560
self.tsframe.to_csv(path, index=False)
56-
recons = DataFrame.from_csv(path, index_col=None)
57-
assert_almost_equal(self.tsframe.values, recons.values)
61+
62+
with tm.assert_produces_warning(FutureWarning,
63+
check_stacklevel=False):
64+
recons = DataFrame.from_csv(path, index_col=None)
65+
assert_almost_equal(self.tsframe.values, recons.values)
5866

5967
# corner case
6068
dm = DataFrame({'s1': Series(lrange(3), lrange(3)),
6169
's2': Series(lrange(2), lrange(2))})
6270
dm.to_csv(path)
63-
recons = DataFrame.from_csv(path)
64-
assert_frame_equal(dm, recons)
71+
72+
with tm.assert_produces_warning(FutureWarning,
73+
check_stacklevel=False):
74+
recons = DataFrame.from_csv(path)
75+
assert_frame_equal(dm, recons)
6576

6677
def test_to_csv_from_csv2(self):
6778

@@ -71,28 +82,36 @@ def test_to_csv_from_csv2(self):
7182
df = DataFrame(np.random.randn(3, 3), index=['a', 'a', 'b'],
7283
columns=['x', 'y', 'z'])
7384
df.to_csv(path)
74-
result = DataFrame.from_csv(path)
75-
assert_frame_equal(result, df)
85+
86+
with tm.assert_produces_warning(FutureWarning,
87+
check_stacklevel=False):
88+
result = DataFrame.from_csv(path)
89+
assert_frame_equal(result, df)
7690

7791
midx = MultiIndex.from_tuples(
7892
[('A', 1, 2), ('A', 1, 2), ('B', 1, 2)])
7993
df = DataFrame(np.random.randn(3, 3), index=midx,
8094
columns=['x', 'y', 'z'])
8195
df.to_csv(path)
82-
result = DataFrame.from_csv(path, index_col=[0, 1, 2],
83-
parse_dates=False)
96+
97+
with tm.assert_produces_warning(FutureWarning,
98+
check_stacklevel=False):
99+
result = DataFrame.from_csv(path, index_col=[0, 1, 2],
100+
parse_dates=False)
84101
# TODO from_csv names index ['Unnamed: 1', 'Unnamed: 2'] should it
85102
# ?
86103
assert_frame_equal(result, df, check_names=False)
87104

88105
# column aliases
89106
col_aliases = Index(['AA', 'X', 'Y', 'Z'])
90107
self.frame2.to_csv(path, header=col_aliases)
91-
rs = DataFrame.from_csv(path)
92-
xp = self.frame2.copy()
93-
xp.columns = col_aliases
94108

95-
assert_frame_equal(xp, rs)
109+
with tm.assert_produces_warning(FutureWarning,
110+
check_stacklevel=False):
111+
rs = DataFrame.from_csv(path)
112+
xp = self.frame2.copy()
113+
xp.columns = col_aliases
114+
assert_frame_equal(xp, rs)
96115

97116
pytest.raises(ValueError, self.frame2.to_csv, path,
98117
header=['AA', 'X'])
@@ -231,10 +250,13 @@ def make_dtnat_arr(n, nnat=None):
231250
with ensure_clean('1.csv') as pth:
232251
df = DataFrame(dict(a=s1, b=s2))
233252
df.to_csv(pth, chunksize=chunksize)
234-
recons = DataFrame.from_csv(pth)._convert(datetime=True,
235-
coerce=True)
236-
assert_frame_equal(df, recons, check_names=False,
237-
check_less_precise=True)
253+
254+
with tm.assert_produces_warning(FutureWarning,
255+
check_stacklevel=False):
256+
recons = DataFrame.from_csv(pth)._convert(datetime=True,
257+
coerce=True)
258+
assert_frame_equal(df, recons, check_names=False,
259+
check_less_precise=True)
238260

239261
@pytest.mark.slow
240262
def test_to_csv_moar(self):
@@ -250,13 +272,19 @@ def _do_test(df, r_dtype=None, c_dtype=None,
250272
with ensure_clean('__tmp_to_csv_moar__') as path:
251273
df.to_csv(path, encoding='utf8',
252274
chunksize=chunksize, tupleize_cols=False)
253-
recons = DataFrame.from_csv(
254-
path, tupleize_cols=False, **kwargs)
275+
276+
with tm.assert_produces_warning(FutureWarning,
277+
check_stacklevel=False):
278+
recons = DataFrame.from_csv(
279+
path, tupleize_cols=False, **kwargs)
255280
else:
256281
kwargs['header'] = 0
257282
with ensure_clean('__tmp_to_csv_moar__') as path:
258283
df.to_csv(path, encoding='utf8', chunksize=chunksize)
259-
recons = DataFrame.from_csv(path, **kwargs)
284+
285+
with tm.assert_produces_warning(FutureWarning,
286+
check_stacklevel=False):
287+
recons = DataFrame.from_csv(path, **kwargs)
260288

261289
def _to_uni(x):
262290
if not isinstance(x, compat.text_type):
@@ -398,12 +426,15 @@ def test_to_csv_from_csv_w_some_infs(self):
398426

399427
with ensure_clean() as path:
400428
self.frame.to_csv(path)
401-
recons = DataFrame.from_csv(path)
402429

403-
# TODO to_csv drops column name
404-
assert_frame_equal(self.frame, recons, check_names=False)
405-
assert_frame_equal(np.isinf(self.frame),
406-
np.isinf(recons), check_names=False)
430+
with tm.assert_produces_warning(FutureWarning,
431+
check_stacklevel=False):
432+
recons = DataFrame.from_csv(path)
433+
434+
# TODO to_csv drops column name
435+
assert_frame_equal(self.frame, recons, check_names=False)
436+
assert_frame_equal(np.isinf(self.frame),
437+
np.isinf(recons), check_names=False)
407438

408439
def test_to_csv_from_csv_w_all_infs(self):
409440

@@ -413,12 +444,15 @@ def test_to_csv_from_csv_w_all_infs(self):
413444

414445
with ensure_clean() as path:
415446
self.frame.to_csv(path)
416-
recons = DataFrame.from_csv(path)
417447

418-
# TODO to_csv drops column name
419-
assert_frame_equal(self.frame, recons, check_names=False)
420-
assert_frame_equal(np.isinf(self.frame),
421-
np.isinf(recons), check_names=False)
448+
with tm.assert_produces_warning(FutureWarning,
449+
check_stacklevel=False):
450+
recons = DataFrame.from_csv(path)
451+
452+
# TODO to_csv drops column name
453+
assert_frame_equal(self.frame, recons, check_names=False)
454+
assert_frame_equal(np.isinf(self.frame),
455+
np.isinf(recons), check_names=False)
422456

423457
def test_to_csv_no_index(self):
424458
# GH 3624, after appending columns, to_csv fails
@@ -448,13 +482,19 @@ def test_to_csv_headers(self):
448482
to_df = DataFrame([[1, 2], [3, 4]], columns=['X', 'Y'])
449483
with ensure_clean('__tmp_to_csv_headers__') as path:
450484
from_df.to_csv(path, header=['X', 'Y'])
451-
recons = DataFrame.from_csv(path)
452-
assert_frame_equal(to_df, recons)
485+
486+
with tm.assert_produces_warning(FutureWarning,
487+
check_stacklevel=False):
488+
recons = DataFrame.from_csv(path)
489+
assert_frame_equal(to_df, recons)
453490

454491
from_df.to_csv(path, index=False, header=['X', 'Y'])
455-
recons = DataFrame.from_csv(path)
456-
recons.reset_index(inplace=True)
457-
assert_frame_equal(to_df, recons)
492+
493+
with tm.assert_produces_warning(FutureWarning,
494+
check_stacklevel=False):
495+
recons = DataFrame.from_csv(path)
496+
recons.reset_index(inplace=True)
497+
assert_frame_equal(to_df, recons)
458498

459499
def test_to_csv_multiindex(self):
460500

@@ -471,11 +511,15 @@ def test_to_csv_multiindex(self):
471511

472512
# round trip
473513
frame.to_csv(path)
474-
df = DataFrame.from_csv(path, index_col=[0, 1], parse_dates=False)
475514

476-
# TODO to_csv drops column name
477-
assert_frame_equal(frame, df, check_names=False)
478-
assert frame.index.names == df.index.names
515+
with tm.assert_produces_warning(FutureWarning,
516+
check_stacklevel=False):
517+
df = DataFrame.from_csv(path, index_col=[0, 1],
518+
parse_dates=False)
519+
520+
# TODO to_csv drops column name
521+
assert_frame_equal(frame, df, check_names=False)
522+
assert frame.index.names == df.index.names
479523

480524
# needed if setUP becomes a classmethod
481525
self.frame.index = old_index
@@ -487,19 +531,28 @@ def test_to_csv_multiindex(self):
487531
tsframe.index = MultiIndex.from_arrays(new_index)
488532

489533
tsframe.to_csv(path, index_label=['time', 'foo'])
490-
recons = DataFrame.from_csv(path, index_col=[0, 1])
491-
# TODO to_csv drops column name
492-
assert_frame_equal(tsframe, recons, check_names=False)
534+
535+
with tm.assert_produces_warning(FutureWarning,
536+
check_stacklevel=False):
537+
recons = DataFrame.from_csv(path, index_col=[0, 1])
538+
# TODO to_csv drops column name
539+
assert_frame_equal(tsframe, recons, check_names=False)
493540

494541
# do not load index
495542
tsframe.to_csv(path)
496-
recons = DataFrame.from_csv(path, index_col=None)
497-
assert len(recons.columns) == len(tsframe.columns) + 2
543+
544+
with tm.assert_produces_warning(FutureWarning,
545+
check_stacklevel=False):
546+
recons = DataFrame.from_csv(path, index_col=None)
547+
assert len(recons.columns) == len(tsframe.columns) + 2
498548

499549
# no index
500550
tsframe.to_csv(path, index=False)
501-
recons = DataFrame.from_csv(path, index_col=None)
502-
assert_almost_equal(recons.values, self.tsframe.values)
551+
552+
with tm.assert_produces_warning(FutureWarning,
553+
check_stacklevel=False):
554+
recons = DataFrame.from_csv(path, index_col=None)
555+
assert_almost_equal(recons.values, self.tsframe.values)
503556

504557
# needed if setUP becomes classmethod
505558
self.tsframe.index = old_index
@@ -599,12 +652,15 @@ def _make_frame(names=None):
599652
with ensure_clean('__tmp_to_csv_multiindex__') as path:
600653
# empty
601654
tsframe[:0].to_csv(path)
602-
recons = DataFrame.from_csv(path)
603-
exp = tsframe[:0]
604-
exp.index = []
605655

606-
tm.assert_index_equal(recons.columns, exp.columns)
607-
assert len(recons) == 0
656+
with tm.assert_produces_warning(FutureWarning,
657+
check_stacklevel=False):
658+
recons = DataFrame.from_csv(path)
659+
exp = tsframe[:0]
660+
exp.index = []
661+
662+
tm.assert_index_equal(recons.columns, exp.columns)
663+
assert len(recons) == 0
608664

609665
def test_to_csv_float32_nanrep(self):
610666
df = DataFrame(np.random.randn(1, 4).astype(np.float32))
@@ -624,8 +680,11 @@ def test_to_csv_withcommas(self):
624680

625681
with ensure_clean('__tmp_to_csv_withcommas__.csv') as path:
626682
df.to_csv(path)
627-
df2 = DataFrame.from_csv(path)
628-
assert_frame_equal(df2, df)
683+
684+
with tm.assert_produces_warning(FutureWarning,
685+
check_stacklevel=False):
686+
df2 = DataFrame.from_csv(path)
687+
assert_frame_equal(df2, df)
629688

630689
def test_to_csv_mixed(self):
631690

@@ -739,8 +798,11 @@ def test_to_csv_wide_frame_formatting(self):
739798

740799
def test_to_csv_bug(self):
741800
f1 = StringIO('a,1.0\nb,2.0')
742-
df = DataFrame.from_csv(f1, header=None)
743-
newdf = DataFrame({'t': df[df.columns[0]]})
801+
802+
with tm.assert_produces_warning(FutureWarning,
803+
check_stacklevel=False):
804+
df = DataFrame.from_csv(f1, header=None)
805+
newdf = DataFrame({'t': df[df.columns[0]]})
744806

745807
with ensure_clean() as path:
746808
newdf.to_csv(path)

0 commit comments

Comments
 (0)