pandas convert mm/dd/yyyy hh:mm to date [duplicate] - python-3.x

This question already has answers here:
removing time from date&time variable in pandas?
(3 answers)
Closed 2 years ago.
I have the time column in a df
accrual_time_x
1/4/19 20:32
trying to convert into
%m/%d/%y
df["accrual_time_x"] = pd.to_datetime(df["accrual_time_x"],format="%m/%d/%y")
yet still return
0 2019-01-04 20:32:00
How could I convert into 01-04-2019? thanks!

Is this what you want?
df['accrual_time_x'].apply(pd.to_datetime).dt.strftime('%m-%d-%Y')

Related

How to select first row after each 3 rows pandas [duplicate]

This question already has answers here:
Pandas every nth row
(7 answers)
Closed 1 year ago.
I have one dataframe, i want to get first row of each 3 rows in dataframe and save new dataframe
here is input data
df=pd.DataFrame({'x':[1,2,5,6,7,8,9,9,6]})
output:
df_out=pd.DataFrame({'x':[1,6,9]})
Use DataFrame.iloc with slicing:
print (df.iloc[::3])
x
0 1
3 6
6 9

How to convert Python Dataframe utc time column to epoch timestamp? [duplicate]

This question already has answers here:
pandas datetime to unix timestamp seconds
(5 answers)
Closed 1 year ago.
I have a df with time column in ISO Format
df = pd.DataFrame({'time':['2021-05-01T16:08:59.094953+00:00','2021-05-01T16:08:56.675183+00:00','2021-05-01T16:08:56.675183+00:00']})
How can i convert this time column to epoch timestamp in Python?
Desired result:
TRY this approach
pd.to_datetime(df.time).astype(int) / 10**9

pandas time series offset in months using frequencies notation [duplicate]

This question already has an answer here:
How can I get pandas Timestamp offset by certain amount of months?
(1 answer)
Closed 2 years ago.
I'd like to offset a date in a DatetimeIndex by a certain number of months using a string but can only find reference to the MonthEnd version.
Here is my (incorrect) attempt
rng = pd.date_range('2020-01-01','2020-07-03')
rng[-1] - pd.tseries.frequencies.to_offset('2m')
This will return Timestamp('2020-05-31 00:00:00') when instead I'm looking for Timestamp('2020-05-03 00:00:00')
Is there an alternative to '2m' here which will give me that result?
The DateOffset method in pandas works well:
rng[-1] - pd.DateOffset(months=2)
Output:
Timestamp('2020-05-03 00:00:00')

how to convert date in month python? [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
i m currently working in Generating dynamic invoice on monthly basis but i m stuck in getting month name in python from user input date
Example: Given Date is :15/03/2020 is date and month is March
Any help would be appreciated
Assuming that you are receiving the date in the form of a string like "15/03/2020":
int(date.split('/')[1]) returns the value between the first and second / of date as an integer. This can then be used as the index for a list of month names to return the name of the month:
months = ["January","February","March",...]
print(months[int(date.split('/')[1])-1])

NA values on Dataframe [duplicate]

This question already has answers here:
How to drop rows of Pandas DataFrame whose value in a certain column is NaN
(15 answers)
How to replace NaN values by Zeroes in a column of a Pandas Dataframe?
(17 answers)
Closed 4 years ago.
so I'm working with python and data frames.
I have a list of movies from which I create a subset and I'm trying to plot or just figure out the mean for that matter but there is a lot of missing information and the frame just has an "NA" instead of data.
I use np library to ignore does values but I still get an error saying the type of input is not supported by isfinite
data = pd.read_csv("C:\\Users\Bubbles\Documents\CS241\week 13\movies.csv")
Action = data[(data.Action == 1)]
Action = Action[np.isfinite(Action.budget)]
print(Action.budget.mean())
the budget list would just contain "NA" and integers as possible values

Resources