Skip to content

Commit

Permalink
ENH: preserve column order in read_file (#378) (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmcbr authored and jorisvandenbossche committed Jan 4, 2017
1 parent c7b4acb commit 82bdd06
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion geopandas/io/file.py
Expand Up @@ -14,7 +14,7 @@ def read_file(filename, **kwargs):
*filename* is either the absolute or relative path to the file to be
opened and *kwargs* are keyword args to be passed to the `open` method
in the fiona library when opening the file. For more information on
in the fiona library when opening the file. For more information on
possible keywords, type: ``import fiona; help(fiona.open)``
"""
bbox = kwargs.pop('bbox', None)
Expand All @@ -27,6 +27,10 @@ def read_file(filename, **kwargs):
f_filt = f
gdf = GeoDataFrame.from_features(f_filt, crs=crs)

# re-order with column order from metadata, with geometry last
columns = list(f.meta["schema"]["properties"]) + ["geometry"]
gdf = gdf[columns]

return gdf


Expand Down
4 changes: 4 additions & 0 deletions geopandas/io/tests/test_io.py
Expand Up @@ -14,6 +14,7 @@ def setUp(self):
self.df = read_file(nybb_zip_path, vfs=vfs)
with fiona.open(nybb_zip_path, vfs=vfs) as f:
self.crs = f.crs
self.columns = list(f.meta["schema"]["properties"].keys())

def test_read_postgis_default(self):
con = connect('test_geopandas')
Expand Down Expand Up @@ -54,6 +55,9 @@ def test_read_file(self):
df = self.df.rename(columns=lambda x: x.lower())
validate_boro_df(self, df)
self.assert_(df.crs == self.crs)
# get lower case columns, and exclude geometry column from comparison
lower_columns = [c.lower() for c in self.columns]
self.assert_((df.columns[:-1] == lower_columns).all())

def test_filtered_read_file(self):
full_df_shape = self.df.shape
Expand Down

0 comments on commit 82bdd06

Please sign in to comment.