I am desperately trying to find a solution for creating a bar chart with plotly express and datetime values as x axis input. The graph is created but remains empty. Without determining the axis I get a proper graph. Does anyone know a solution for that?
import plotly.express as px
print(type(df.index))
fig = px.bar(df, x=df.index, y=df['moneyness'])
fig.show()
My dataframe looks like the following:
{'trade_seq': {Timestamp('2022-01-01 00:16:21.725000+0000', tz='UTC'): 3,
Timestamp('2022-01-01 00:19:23.104000+0000', tz='UTC'): 227,
Timestamp('2022-01-01 00:23:27.841000+0000', tz='UTC'): 228,
Timestamp('2022-01-01 00:28:56.438000+0000', tz='UTC'): 183,
Timestamp('2022-01-01 00:43:06.220000+0000', tz='UTC'): 184},
....
'moneyness': {Timestamp('2022-01-01 00:16:21.725000+0000', tz='UTC'): -4310.019999999997,
Timestamp('2022-01-01 00:19:23.104000+0000', tz='UTC'): 761.9400000000023,
Timestamp('2022-01-01 00:23:27.841000+0000', tz='UTC'): 631.0899999999965,
Timestamp('2022-01-01 00:28:56.438000+0000', tz='UTC'): -355.2699999999968,
Timestamp('2022-01-01 00:43:06.220000+0000', tz='UTC'): -646.7200000000012}}
Related
I have the following code that display streamlit bar chart for one of our job. How do I display RED for error and GREEN for success ?. Each run tell us how long it took in mins and whether it is successful or not. 'S' = Success, 'F' = Fail. As you can see, i only able to show all color green. How do i use alt.condition to switch between bar color.
Second question is to how do I set the graph background to WHITE color not following the body background color which is now in lightgray ?
df = pd.DataFrame({
'runid': ['983232','98324', '1032019', '1042019',
'1052019','1062019', '1072019', '1082019',
'1092019','10102019', '10112019', '10122019',
'10132019','10142019', '10152019', '10162019'],
'duration': [5, 30, 3, 10,3.4,6,12,6,3,6,3,1,5,4.3,4,3],
'results': ['S','S','S','S','S','S','S','S','S','F','S','S','F','S','S','S']
})
c = alt.Chart(df).mark_bar().encode(
x='runid', y='duration', tooltip=['runid', 'duration'],
color=alt.value('#32a852'))
st.altair_chart(c, use_container_width=True)
You can try this:
df = pd.DataFrame({
'runid': ['983232','98324', '1032019', '1042019',
'1052019','1062019', '1072019', '1082019',
'1092019','10102019', '10112019', '10122019',
'10132019','10142019', '10152019', '10162019'],
'duration': [5, 30, 3, 10,3.4,6,12,6,3,6,3,1,5,4.3,4,3],
'results': ['S','S','S','S','S','S','S','S','S','F','S','S','F','S','S','S']
})
c = alt.Chart(df).mark_bar().encode(x='runid', y='duration', tooltip=['runid', 'duration'],
color=alt.condition(
alt.datum.results == "S",
alt.value("#32a852"),
alt.value("red")
)).configure(background='#FFFFFF')
st.altair_chart(c, use_container_width=True)
I'm trying to map a column 'eventtimestamp' to its day of week with the following function:
from datetime import datetime
import calendar
from pyspark.sql.functions import UserDefinedFunction as udf
def toWeekDay(x):
v = int(datetime.strptime(str(x),'%Y-%m-%d %H:%M:%S').strftime('%w'))
if v == 0:
v = 6
else:
v = v-1
return calendar.day_name[v]
and for my df trying to create a new column dow with UDF.
udf_toWeekDay = udf(lambda x: toWeekDay(x), StringType())
df = df.withColumn("dow",udf_toWeekDay('eventtimestamp'))
Yet, I'm getting error I do not understand at all. Firstly, it was complaining for inserting datetime.datetime into strptime instead of string. So I parsed to str and now I don't have a clue what's wrong.
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-9040214714346906648.py", line 267, in <module>
raise Exception(traceback.format_exc())
Exception: Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-9040214714346906648.py", line 260, in <module>
exec(code)
File "<stdin>", line 10, in <module>
File "/usr/lib/spark/python/pyspark/sql/dataframe.py", line 429, in take
return self.limit(num).collect()
File "/usr/lib/spark/python/pyspark/sql/dataframe.py", line 391, in collect
port = self._jdf.collectToPython()
File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
answer, self.gateway_client, self.target_id, self.name)
File "/usr/lib/spark/python/pyspark/sql/utils.py", line 63, in deco
return f(*a, **kw)
File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py", line 319, in get_return_value
format(target_id, ".", name), value)
Py4JJavaError: An error occurred while calling o6250.collectToPython.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 1107.0 failed 4 times, most recent failure: Lost task 0.3 in stage 1107.0 (TID 63757, ip-172-31-27-113.eu-west-1.compute.internal, executor 819): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
Thanks a lot for clues!
we can use date_format to get dayofweek,
df = df.withColumn("dow",date_format(df['eventtimestamp'],'EEEE'))
Attempting to extend expiration on a list of hits per API instructions
for hit_id in expired_hit_list:
response = client.update_expiration_for_hit(
HITId=hit_id,
ExpireAt=datetime(2017, 4, 9, 19, 9, 41, tzinfo=tzlocal())
)
getting error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-59-e0764e20a54b> in <module>()
2 response = client.update_expiration_for_hit(
3 HITId=hit_id,
----> 4 ExpireAt=datetime(2017, 4, 9, 19, 9, 41, tzinfo=tzlocal())
5 )
NameError: name 'datetime' is not defined
I also tried datetime.datetime and dateTime and also just removing it.
ExpireAt=(2017, 4, 9, 19, 9, 41, tzinfo=tzlocal())
nothing working. Suggestions?
it's just an issue with my Python setup, nothing to do with boto3
import datetime
from dateutil.tz import tzlocal
I am trying to get a googlecharts line graph to show me two line graphs with a Y axis of date and an x axis of total amount of substance used. It will be a line graph comparing, for example the total amount of alcohol consumed to tobacco consumed in total per each day.
The area i'm struggling with is figuring out how to show these lines more closely in relation to each other. The amount of total alcohol consumed in a day might be something like 1000mL with a range of 900 to 1100 while tobacco might just be something like 3 with a range of 2 - 4. As you can see, on a line graph these lines will be very far away from each other, and not really showing the correlation.
What would be the best system to do this with?
What you want is a combo chart. I started with the example code from the Combo Chart, changed the last column, and added the second axis. The parameter you care most about is the targetAxisIndex parameter.
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Papua New Guinea', 'Rwanda', 'Average'],
['2004/05', 165, 938, 522, 998, 450, 614.6/100],
['2005/06', 135, 1120, 599, 1268, 288, 682/100],
['2006/07', 157, 1167, 587, 807, 397, 623/100],
['2007/08', 139, 1110, 615, 968, 215, 609.4/100],
['2008/09', 136, 691, 629, 1026, 366, 569.6/100]
]);
// Create and draw the visualization.
var ac = new google.visualization.ComboChart(document.getElementById('visualization'));
ac.draw(data, {
title : 'Monthly Coffee Production by Country',
width: 600,
height: 400,
vAxis: {title: "Cups"},
hAxis: {title: "Month"},
seriesType: "bars",
series: {5: {type: "line", "targetAxisIndex": "1"}}
});
}
When I am trying to use sqlite3_exec to insert new data into the database, it returns error 14 (SQLITE_CANTOPEN). But when I am using sqlite3_prepare_v2 to select, it works fine. Is there an issue with permissions? How to fix it?
sprintf(temp, "INSERT INTO owned (pid, oname, okey, ohp, oatt, odef) VALUES (%d, %c%s%c, %c%s%c, %d, %d, %d);", pid, 34, poname, 34, 34, passkey, 34, sqlite3_column_int(res3,0), sqlite3_column_int(res3,1), sqlite3_column_int(res3,2));
error = sqlite3_exec(conn, temp, 0, 0, 0);