Extract Month Number from DatetimeIndex in Pandas

AmitDiwan
Updated on 19-Oct-2021 08:45:04

1K+ Views

To extract month number from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.month property.At first, import the required libraries −import pandas as pdDatetimeIndex with period 6 and frequency as M i.e. month. The timezone is Australia/Sydney −datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='M')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Get the month number i.e. January=1, february=2, ..., December=12 −print("Getting the month number..", datetimeindex.month) ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 6 and frequency as M i.e. month # timezone is Australia/Sydney datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='M') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) # ... Read More

Extract Year from DatetimeIndex in Python Pandas

AmitDiwan
Updated on 19-Oct-2021 08:42:06

2K+ Views

To extract year from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.year property.At first, import the required libraries −import pandas as pdDatetimeIndex with period 6 and frequency as Y i.e. years. The timezone is Australia/Sydney −datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='Y')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Get the year −print("Getting the year name..", datetimeindex.year) ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 6 and frequency as Y i.e. years # timezone is Australia/Sydney datetimeindex = pd.date_range('2021-09-24 02:35:55', periods=6, tz='Australia/Sydney', freq='Y') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...", datetimeindex.freq) ... Read More

Curve Text in a Polar Plot in Matplotlib

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:41:45

855 Views

To curve text in a polar plot in matplotlib we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Plot the line with some degree, color='green' and linewidth=2.Create x and y data points, with some curve and plot them using plot() method.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt from scipy.interpolate import interp1d import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax ... Read More

Create Datetime with DatetimeIndex in Python Pandas

AmitDiwan
Updated on 19-Oct-2021 08:38:54

5K+ Views

To create a datetime, we will use the date_range(). The periods and the time zone will also be set with the frequency. At first, import the required libraries −import pandas as pdDatetimeIndex with period 8 and frequency as M i.e. months. The timezone is Australia/Sydney −datetime = pd.date_range('2021-09-24 02:35:55', periods=8, tz='Australia/Sydney', freq='M') Display the datetime −print("DateTime...", datetime)ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 8 and frequency as M i.e. months # timezone is Australia/Sydney datetime = pd.date_range('2021-09-24 02:35:55', periods=8, tz='Australia/Sydney', freq='M') # display print("DateTime...", datetime) # get the day name print("Getting the ... Read More

Get X, Y Position from Mouse in Interactive Plot using Matplotlib

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:36:26

5K+ Views

To get the (x, y) positions pointing with mouse in an interactive plot, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Bind the function *mouse_event* to the event *button_press_event*.Create x and y data points using numpy.Plot the x and y data points using plot() method.To display the figure, use Show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def mouse_event(event): print('x: {} and y: {}'.format(event.xdata, event.ydata)) fig = ... Read More

Return Vector of Label Values Using Level Name in MultiIndex

AmitDiwan
Updated on 19-Oct-2021 08:36:19

179 Views

To return vector of label values using level name in the MultiIndex, use the MultiIndex.get_level_values() method in Pandas.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects −multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')], names=['One', 'Two'])Display the MultiIndex −print("The MultiIndex...", multiIndex) Get level values using level name "Two" −print("Level values using level name...", multiIndex.get_level_values("Two"))ExampleFollowing is the code −import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')], names=['One', 'Two']) # display the MultiIndex print("The MultiIndex...", multiIndex) # get the levels ... Read More

Return Vector of Label Values Using Integer Position in MultiIndex

AmitDiwan
Updated on 19-Oct-2021 08:34:31

225 Views

To return vector of label values using integer position of the level in the MultiIndex, use the MultiIndex.get_level_values() method in Pandas. Set the level as an argument.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects −multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')], names=['One', 'Two']) Display the MultiIndex −print("The MultiIndex...", multiIndex)Get level values at level 0 −print("Level values at level 0...", multiIndex.get_level_values(0)) ExampleFollowing is the code −import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')], names=['One', 'Two']) # display ... Read More

Return Vector of Label Values for Requested Level in a MultiIndex

AmitDiwan
Updated on 19-Oct-2021 08:31:01

306 Views

To return vector of label values for requested level in a MultiIndex, use the multiIndex.get_level_values() method. Set the level name as an argument.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects −multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')], names=['One', 'Two'])Display the MultiIndex −print("The MultiIndex...", multiIndex) Get the levels in MultiIndex −print("The levels in MultiIndex...", multiIndex.levels)Get level values at level 0 −print("Level values...", multiIndex.get_level_values(0)) ExampleFollowing is the code −import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')], names=['One', 'Two']) # ... Read More

Put XTick Labels in a Box Matplotlib

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:29:23

641 Views

To put xtick labels in a box, we can take the following stepsStepsCreate a new figure or activate an existing figure.Get the current axis of the figure.Set the left and bottom position of the axes.Set the position of the spines, i.e., bottom and left.To put xtick labels in a box, iterate the ticklabels and use set_bbox() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.figure() ax = plt.gca() ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data', 0)) ax.spines['left'].set_position(('data', 0)) for label in ax.get_xticklabels():    label.set_fontsize(12)    label.set_bbox(dict(facecolor='red', edgecolor='black', alpha=0.7)) ... Read More

Plot Time as Index Value in Pandas DataFrame using Matplotlib

Rishikesh Kumar Rishi
Updated on 19-Oct-2021 08:28:32

2K+ Views

To plot a time as an index value in a Pandas dataframe in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a Pandas dataframe with two columns, time and speed.Set the DataFrame index using existing columns.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt import pandas as pd import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Pandas dataframe df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10))) # Set the dataframe index df.set_index('time').plot() # ... Read More

Advertisements