Skip to content

Commit 9eea984

Browse files
authored
Fix 'Station' KeyError in Stations.collect_locations() (#52) (Closes #51)
1 parent 02f94ca commit 9eea984

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

docs/source/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
furo==2024.8.6
1+
furo==2025.7.19
22
pyhelpers==2.3.0
33
sphinx-copybutton==0.5.2
44
sphinx-new-tab-link==0.8.0

pyrcs/other_assets/station.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def _check_row_spans(dat):
8484
dat0[['ELR', 'Mileage']] = dat0[['ELR', 'Mileage']].map(lambda x: x.split(' &&& '))
8585
dat0 = dat0.explode(['ELR', 'Mileage'], ignore_index=True)
8686

87-
dat0.sort_values(['Station'], ignore_index=True, inplace=True)
88-
8987
return dat0
9088

9189

@@ -122,22 +120,28 @@ def _parse_station_column(dat):
122120
x = 'Heathrow Junction [sometimes referred to as Heathrow Interchange]\t\t / [no CRS?]'
123121
"""
124122

125-
temp1 = dat['Station'].str.split('\t\t', n=1, expand=True)
126-
temp1.columns = ['Station', 'CRS']
127-
dat['Station'] = temp1['Station'].str.rstrip(' / ').str.strip()
123+
stn_col_name = [col for col in dat.columns if 'Station' in col][0]
124+
temp1 = dat[stn_col_name].str.split('\t\t', n=1, expand=True)
125+
126+
if stn_col_name != 'Station':
127+
dat.rename(columns={stn_col_name: 'Station'}, inplace=True)
128+
stn_col_name = 'Station'
129+
130+
temp1.columns = [stn_col_name, 'CRS']
131+
dat[stn_col_name] = temp1[stn_col_name].str.rstrip(' / ').str.strip()
128132

129133
# Get notes for stations
130134
stn_note_ = pd.Series('', index=dat.index)
131-
for i, x in enumerate(temp1['Station']):
135+
for i, x in enumerate(temp1[stn_col_name]):
132136
if '[' in x and ']' in x:
133137
y = re.search(r' \[(.*)](✖.*)?', x).group(0) # Station Note
134-
dat.loc[i, 'Station'] = x.replace(y, '').strip()
138+
dat.loc[i, stn_col_name] = str(x).replace(y, '').strip()
135139
if '✖' in y:
136140
stn_note_[i] = '; '.join([y_.strip(' []') for y_ in y.split('✖')])
137141
else:
138142
stn_note_[i] = y.strip(' []')
139143

140-
dat.insert(loc=dat.columns.get_loc('Station') + 1, column='Station Note', value=stn_note_)
144+
dat.insert(loc=dat.columns.get_loc(stn_col_name) + 1, column='Station Note', value=stn_note_)
141145

142146
temp2 = temp1['CRS'].str.replace(' / /', ' &&& ').str.split(
143147
r' | / ', regex=True, expand=True).fillna('')
@@ -154,7 +158,7 @@ def _parse_station_column(dat):
154158
lambda z: ' and '.join(['{} [{}]'.format(*z_.split('✖')) for z_ in z.split(' &&& ')])
155159
if ' &&& ' in z else z).str.strip()
156160

157-
dat = pd.concat([dat, temp2], axis=1)
161+
dat = pd.concat([dat, temp2], axis=1).sort_values(stn_col_name)
158162

159163
return dat
160164

requirements.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
build==1.2.2.post1
1+
build==1.3.0
22
fqdn==1.5.1
3-
furo==2024.8.6
3+
furo==2025.7.19
44
isoduration==20.11.0
55
jaraco.collections==5.1.0
66
jsonpointer==3.0.0
7-
notebook==7.4.4
7+
notebook==7.4.5
8+
pandas-stubs==2.3.2.250827
89
pip-chill==1.0.3
910
pkginfo==1.12.1.2
1011
pyhelpers==2.3.0
@@ -15,6 +16,6 @@ sphinx-new-tab-link==0.8.0
1516
sphinx-toggleprompt==0.6.0
1617
tinycss2==1.4.0
1718
tomli==2.0.1
18-
twine==6.1.0
19+
twine==6.2.0
1920
uri-template==1.3.0
2021
webcolors==24.11.1

0 commit comments

Comments
 (0)