Skip to content

Commit be16b70

Browse files
author
William
committed
Fix pd.to_numeric bug
1 parent 9c48966 commit be16b70

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

Streamlit_Mapping.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ def button9_click():
491491
valid_input = False
492492
if valid_input:
493493
df = df.rename(columns={df.columns[st.session_state.index + 1]: new_title})
494-
495494
if mapname != list(df.columns[1:]):
496495
st.session_state.df = df
497496
st.session_state.mapname = list(df.columns[1:])
@@ -553,11 +552,14 @@ def button9_click():
553552
# Colour change options
554553
discrete_colours = st.sidebar.toggle(label='Use discrete colouring')
555554
num_colours = st.sidebar.slider("Number of Colours", min_value=2, max_value=6, value=5)
556-
555+
557556
if not df.empty and 'index' in st.session_state and mapname:
558-
df[mapname[st.session_state.index]] = (df[mapname[st.session_state.index]].astype(str).str.replace(r"[^\d.-]", "", regex=True))
557+
df[mapname[st.session_state.index]] = (
558+
df[mapname[st.session_state.index]]
559+
.astype(str)
560+
.str.replace(r"[£$€,]", "", regex=True)
561+
)
559562
df[mapname[st.session_state.index]] = pd.to_numeric(df[mapname[st.session_state.index]], errors="coerce")
560-
561563
# Colour pickers
562564
colours = []
563565
# Create two columns in the sidebar using container
@@ -653,7 +655,6 @@ def button9_click():
653655
with colour_column3:
654656
thresholds[i] = st.number_input('<', float(thresholds[i-1] + step/10), value=float(thresholds[i]), step=step, key=f'+input{i}', label_visibility="hidden", format=f'%.{dp}f')
655657
colours.append(colour)
656-
# print(float(thresholds[i]))
657658
custom_colour_scale = colours
658659
else:
659660
thresholds=[]
@@ -739,6 +740,7 @@ def button9_click():
739740
st.rerun()
740741
if query_params['preset'] == 'ln_average_growth':
741742
df = pd.read_csv("examples/ITL3_LN_Average_Growth.csv")
743+
742744
fig = True
743745
mapname = df.columns[1:].tolist()
744746
levels = []

examples/ITL3_LN_Average_Growth.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ TLK51,-0.003623125648410171
8383
TLK52,0.012095766871986886
8484
TLK61,0.012261231961902356
8585
TLK62,0.0032346655676786184
86-
TLK63,6.816284108642397e-06
86+
TLK63,0.000006816284109
8787
TLK64,-0.005569673784010772
8888
TLK71,0.0036349922136757586
8989
TLK72,0.0033324009174603357
@@ -117,7 +117,7 @@ TLI45,-0.001946969498128759
117117
TLI51,-0.016438004922871487
118118
TLI52,-0.015066114774994628
119119
TLI53,-0.020972111070233387
120-
TLI54,1.3986503075891222e-05
120+
TLI54,0.0000139865030759
121121
TLI61,-0.006674786791348867
122122
TLI62,0.009164732708573571
123123
TLI63,-0.0013626803062413372
@@ -144,7 +144,7 @@ TLF14,-0.0009582513285482838
144144
TLF15,0.002022490388451104
145145
TLF16,0.009590992767239234
146146
TLF21,0.005606386669048765
147-
TLF22,6.509781463120608e-05
147+
TLF22,0.000065097814631
148148
TLF24,0.00492114020948217
149149
TLF25,-0.0069685070962031284
150150
TLF30,0.003936685716320673

map.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def make_choropleths(data, map_df, geo_level, colorscale=sequential.Viridis[::-1
6262

6363
column = data.columns[index]
6464
temp = data[column]
65-
temp = (temp.astype(str).str.replace(r"[^\d.-]", "", regex=True))
65+
temp = (temp.astype(str).str.replace(r"[£$€,]", "", regex=True))
6666
temp = pd.to_numeric(temp, errors="coerce")
6767

6868
hovertemplate = '%{text}<br>' + column + f': {unit}'+'%{customdata[0]:' + data_format + '}<extra></extra>'

0 commit comments

Comments
 (0)