How to make a numpy arange of datetime without a specific year? - python

I have data of days from all days from 2004-01-01 until 2015-31-12 and want to plot the maximum and the minimun value of each day.
The original data is on df and df['Day'] is a colum with day and month.
So, I created two new dataframes:
dfmin=df.groupby('Day').agg('min')
dfmax=df.groupby('Day').agg('max')
The new dataframes has one row for each day of the year, considering the max and the minimun value for each day in the range.
I want to label the axis with each day, but without specify any year.
I already saw this questions and this documentation but did not find the answer.
For example, I did:
observation_dates = np.arange('2013-01-01', '2014-01-01', dtype='datetime64[D]')
plt.plot(dfmin.index, dfmin.Data_Value)
plt.plot(dfmin.index, dfmax.Data_Value)
...
And created the following chart:
But I would like to do something like:
observation_dates = np.arange(' -01-01', ' -01-01', dtype='datetime64[D]')
...
So the axis would be labeled just with the days, but without specifying any year
EDIT TO CLARIFY A LITTLE MORE:
After group the data by days, I got the following dataframe (represented by the blue line at the chart):
DAY Data_Value
01-01 -160
01-02 -267
01-03 -267
I just want to plot this values using dates at x-axis

Related

How to plot with time series data in multindex dataframe?

I am trying to plot hypothetical yearly data using pandas or matlib.
I have the dataframe as below
Data Frame
Here month day are indexes and minute, value are simple columns. I have tried doing
df.loc['AMD'].plot(x=col[0], y=col[1])
where col[0] is minute and col1 is values.
But what happens is that it superimposes all 00:00 values of all days at one place. So basically the plot is for one day only and all values are superimposed.
Can you guide me how I can plot it properly for full year?

How to show dates instead of months in plots for a column with datetime type in Python?

I have a dataframe with column : Week and Spend.
For instance :
Week Spend
2019-01-13 600
2018-12-30 400
The Week column is in datetime format.
When I create a line chart to see the trend, I get the plot with x axis labels as following:
I want the dates to be shown as is, instead of being aggregated as months. This is because I want to see the weekly change.

How to label x-axis with date in month-day format?

I have data of all days through 5 years. I wnat to plot the maxmium value of each day. So, I grouped the data to a DataFrame with 365 rows:
dfmax=df.groupby('Day').agg('max')
And got the following dataframe:
Data_Value
Day
01-01 150
01-02 180
01-03 160
I want to plot labeling the x-axis with the dates, but the code:
plt.plot(dfmax.index,dfmax.Data_Value)
Returned an error: ValueError: could not convert string to float: '12-31'
I know that I have to convert index to datetime format, and saw some answers, but did not find a way to do it without sepcify an year at the label. I want the labes at month-day format
EDIT:
An example to reproduce the error:
df=pd.DataFrame({'Day':['01-01', '02-01','03-01'], 'Value':[1,6,3]})
plt.figure()
plt.plot(df.Day, df.Value)
plt.show()

pandas: MultiIndex not showing when plotting DataFrame

I am plotting the following pandas MultiIndex DataFrame:
print(log_returns_weekly.head())
AAPL MSFT TSLA FB GOOGL
Date Date
2016 1 -0.079078 0.005278 -0.155689 0.093245 0.002512
2 -0.001288 -0.072344 0.003811 -0.048291 -0.059711
3 0.119746 0.082036 0.179948 0.064994 0.061744
4 -0.150731 -0.102087 0.046722 0.030044 -0.074852
5 0.069314 0.067842 -0.075598 0.010407 0.056264
with the first sub-index representing the year, and the second one the week from that specific year.
This is simply achieved via the pandas plot() method; however, as seen below, the x-axis will not be in a (year, week) format i.e. (2016, 1), (2016, 2) etc. Instead, it simply shows 'Date,Date' - does anyone therefore know how I can overcome this issue?
log_returns_weekly.plot(figsize(8,8))
You need to convert your multiindex to single one and add a day, so it would be like this: 2016-01-01.
log1 = log_returns_weekly.set_index(log_returns_weekly.index.map(lambda x: pd.datetime(*x,1)))
log1.plot()

Python: Date conversion to year-weeknumber, issue at switch of year

I am trying to convert a dataframe column with a date and timestamp to a year-weeknumber format, i.e., 01-05-2017 03:44 = 2017-1. This is pretty easy, however, I am stuck at dates that are in a new year, yet their weeknumber is still the last week of the previous year. The same thing that happens here.
I did the following:
df['WEEK_NUMBER'] = df.date.dt.year.astype(str).str.cat(df.date.dt.week.astype(str), sep='-')
Where df['date'] is a very large column with date and times, ranging over multiple years.
A date which gives a problem is for example:
Timestamp('2017-01-01 02:11:27')
The output for my code will be 2017-52, while it should be 2016-52. Since the data covers multiple years, and weeknumbers and their corresponding dates change every year, I cannot simply subtract a few days.
Does anybody have an idea of how to fix this? Thanks!
Replace df.date.dt.year by this:
(df.date.dt.year- ((df.date.dt.week>50) & (df.date.dt.month==1)))
Basically, it means that you will substract 1 to the year value if the week number is greater than 50 and the month is January.

Categories

Resources