Converting string to datetime format shows yyyy/dd/mm for first 12 rows and yyyy/mm/dd for remaining rows for each month - datetime-format

Problem: I imported a csv from google drive, selected few columns, applied ''date format'' to 'Date' column and then tried to filter a particular month.
Edit
I'm using Python as programming language.
This is my code I used:
from google.colab import auth
auth.authenticate_user()
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
worksheet = gc.open('Sale_Data_WC_2022').sheet1
# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)
# Convert to a DataFrame and render.
import pandas as pd
df = pd.DataFrame.from_records(rows)
df1 = df.iloc[:,[1,2,5,6,7,8,9]]
df1 = df1.rename(columns={df1.columns[0]: 'Date'})
df1
df1['Date'] = pd.to_datetime(df1['Date'])
df1
Output:
01-01-22 0:00
01-02-22 0:00
01-03-22 0:00
01-04-22 0:00
01-05-22 0:00
01-06-22 0:00
01-07-22 0:00
01-08-22 0:00
01-10-22 0:00
01-11-22 0:00
01-12-22 0:00
13-01-22 0:00
14-01-22 0:00
15-01-22 0:00
17-01-22 0:00
18-01-22 0:00
19-01-22 0:00
20-01-22 0:00
21-01-22 0:00
22-01-22 0:00
24-01-22 0:00
25-01-22 0:00
26-01-22 0:00
27-01-22 0:00
Format shows yyyy-dd-mm for first 12 rows and yyyy-mm-dd for remaining rows for each month.

Related

why open file descriptors are not getting reused instead they are increasing in number value

I have a simple C HTTP server. I close file descriptors for disk files and new connection fds returned by accept(...), but I noticed that I am getting new file descriptor numbers that are bigger than the previous numbers: for example file descriptor from accept return starts with 4, then 5, then 4 again and so on until file descriptor reaches max open file descriptor on a system.
I have set the value to 10,000 on my system but I am not sure why exactly file descriptor number jumps to max value. And I am kind of sure than my program is closing the file descriptors.
So I would like to know if there are not thousands of connections then how come file descriptor new number are increasing periodically: in around 24 hours I get message accept: too many open files. What is this message?
Also, does ulimit -n number value get reset automatically without system reboot?
as mentioned in the answer. The output of _2$ ps aux | grep lh is
dr-x------ 2 fawad fawad 0 Oct 11 11:15 .
dr-xr-xr-x 9 fawad fawad 0 Oct 11 11:15 ..
lrwx------ 1 fawad fawad 64 Oct 11 11:15 0 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:15 1 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:15 2 -> /dev/pts/3
lrwx------ 1 fawad fawad 64 Oct 11 11:25 255 -> /dev/pts/3
and the output of ls -la /proc/$$/fd is
root 49855 0.5 5.4 4930756 322328 ? Sl Oct09 15:58 /usr/share/atom/atom --executed-from=/home/fawad/Desktop/C++-work/lhparse --pid=49844 --no-sandbox
root 80901 0.0 0.0 25360 5952 pts/4 S+ 09:32 0:00 sudo ./lh
root 80902 0.0 0.0 1100852 2812 pts/4 S+ 09:32 0:00 ./lh
fawad 83419 0.0 0.0 19976 916 pts/3 S+ 11:27 0:00 grep --color=auto lh
I like to know what is pts/4 etc. column. is this the file descriptor number.
It's likely that the socket that is represented by the file descriptor is in close_wait or time_wait state. Which means the TCP stack holds the fd open for a bit longer. So you won't be able to reuse it immediately in this instance.
Once the socket is fully finished with and closed, the file descriptor number will then available for reuse inside your program.
See: https://en.m.wikipedia.org/wiki/Transmission_Control_Protocol
Protocol Operation and specifically Wait States.
To see what files are still open you can run
ls -la /proc/$$/fd
The output of this will also be of help.
ss -tan | head -5
LISTEN 0 511 *:80 *:*
SYN-RECV 0 0 192.0.2.145:80 203.0.113.5:35449
SYN-RECV 0 0 192.0.2.145:80 203.0.113.27:53599
ESTAB 0 0 192.0.2.145:80 203.0.113.27:33605
TIME-WAIT 0 0 192.0.2.145:80 203.0.113.47:50685

Why does grepping PID keep changing?

I want to write a bashscript to kill a process by ID, but I can't even seem to get the PID working since it keeps changing. Why is this?
Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof 515 0.0 4.3 4822060 723232 ?? U 10:28PM 4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof 4489 0.0 0.0 2436888 812 s002 S+ 12:36AM 0:00.00 grep firefox
Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof 515 0.0 4.3 4822060 723232 ?? U 10:28PM 4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof 4491 0.0 0.0 2436888 812 s002 S+ 12:36AM 0:00.00 grep firefox
Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof 515 0.0 4.3 4822060 723232 ?? U 10:28PM 4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof 4493 0.0 0.0 2436888 812 s002 S+ 12:36AM 0:00.00 grep firefox
Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof 515 0.0 4.3 4822060 723232 ?? U 10:28PM 4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof 4495 0.0 0.0 2435864 800 s002 S+ 12:36AM 0:00.00 grep firefox
Every time you fire
ps aux | grep "firefox"
you are restarting a grep process. It's doesn't indicate the PID of the running Firefox located at
/Applications/Firefox.app/Contents/MacOS/firefox
in your case.
In your case, 515 is the process to kill.
Each time a process is started, it is assigned a new (incrementing) PID; even if the executable and all arguments are the same.
You'll notice the PID of "firefox" is the same for all of your four calls, indicating that is hasn't been restarted meanwhile.
The PID for "grep" changes as it has been started (and terminated) for each call.

Convert varchar to date in SQL Server 2008

I have two different table for date and time in one application. In date table "date" is stored in "datetime" format and in time table "time" part is stored in varchar format.
Both the table id is stored in transaction table for date time value.
I have an issue while querying the database specifying particular datetime value from transaction table.
Date table
ID_DAT DATE_DAT (smalldatetime)
20000101 01/01/2000 0:00
20000102 02/01/2000 0:00
20000103 03/01/2000 0:00
20000104 04/01/2000 0:00
20000105 05/01/2000 0:00
20000106 06/01/2000 0:00
20000107 07/01/2000 0:00
20000108 08/01/2000 0:00
20000109 09/01/2000 0:00
20000110 10/01/2000 0:00
Time Table
ID_TIM HOUR_TIM MINUTE_TIM STRING_TIM (varchar)
0 0 0 00:00
1 0 1 00:01
2 0 2 00:02
3 0 3 00:03
4 0 4 00:04
5 0 5 00:05
6 0 6 00:06
7 0 7 00:07
8 0 8 00:08
9 0 9 00:09
10 0 10 00:10
Transaction data sample (id may not match with master provided)
SEQNUM ID_DAT ID_TIM ORIGINAL_VALUE_PER
2495089 20130424 30 10.0000000000
2495089 20130424 60 12.0000000000
2495089 20130424 90 15.0000000000
2495089 20130424 120 20.0000000000
2495089 20130424 150 24.0000000000
2495089 20130424 180 28.0000000000
2495089 20130424 210 34.0000000000
now I want to query transaction data let's say after 03:30 for the particular day.
Please guide me how can i achieve the same.
Thanks
Something vaguely like this:
SELECT * from TRANSACTION_TABLE TT
join DATE_TABLE DT on TT.ID_DAT = DT.ID_DAT
join TIME_TABLE TM on TT.ID_TIM = TM.ID_TIM
where (DT.DATE_DAT > '10/19/2013' and DT.DATE_DAT < '10/21/2013')
or (DT.DATE_DAT = '10/21/2013' and HOUR_TIM < 3 and MINUT_TIME < 30)
or (DT.DATE_DAT = '10/19/2013' and HOUR_TIM >= 3 and MINUT_TIME >= 30)

Please explain the ps -aef response on RHEL

What does pts/2 indicate in the below output. Why there is no such for other dd processes?
$ ps -aef |grep dd
root 6553672 15073352 3 02:32:19 - 0:01 dd of=/dev/lv01 bs=1024k
padmin 9437410 16515110 1 02:43:32 **pts/2** 0:00 grep dd
root 13828156 11010220 0 02:32:33 - 0:00 dd of=/dev/lv02 bs=1024k
root 14155860 13828156 2 02:32:33 - 0:01 dd of=/dev/lv02 bs=1024k
root 15073352 13762812 0 02:32:19 - 0:00 dd of=/dev/lv01 bs=1024k
root 15532200 15925276 2 02:40:47 **pts/1** 0:03 dd of=/home/padmin/sample-dd-op bs=1024k
pts/X in the TTY column means that the process is connected to a pseudo terminal slave.
An empty value means:
The terminal session has ended
The command was fired by a daemon
This nice Answer shows the difference between PTS and TTY.

fork() and kill in linux

In my project, I need a sub-task to play in parallel with a parent task. I use fork() to create a parallel process which is aplay xyz.wav. Now when I want to kill the child process from the parent process e.g system("kill -9 aplay ") the aplay is killed, but I see two instances of the parent task. I think one of them is a copy made by the call to fork() and the other one is the original. And with each fork() call, the number of copies increases. I am conscious about memory overflow. I just want to keep the original process. I have tried to kill that parent copy but failed!copies of the parent process appear as 'defunct'.
the 'defucnt' process continue to increase as i call parallel process, whether i kill aplay or not. I also tried to kill defunt by its PID, it also didn't work.
static int
test(Core *pc, char *args)
{
pid1=fork();
if (pid1 ==0)
{
system ("ps " );
system("aplay /opt/Line_Tone_14s.wav");
_exit(0);
}
else if(pid1<0)
{
out("fork() ERROR (-1) returned\n");
}
else if(pid1>0)
{
out("I AM IN PARENT PROCESS() NOW\n");
}
return 1;
}
static int
test1(Core *pc, char *args)
{
system ("ps " );
system ("killall -9 aplay ");
return 1;
}
Initially
> PID TTY TIME CMD
1580 pts/0 00:00:00 sudo
1581 pts/0 00:00:00 su
1589 pts/0 00:00:00 bash
5732 pts/0 00:00:00 parent
5738 pts/0 00:00:00 sh
5739 pts/0 00:00:00 ps
>test
> PID TTY TIME CMD
1580 pts/0 00:00:00 sudo
1581 pts/0 00:00:00 su
1589 pts/0 00:00:00 bash
5732 pts/0 00:00:00 parent
5737 pts/0 00:00:00 parent
5740 pts/0 00:00:00 sh
5741 pts/0 00:00:00 aplay
5743 pts/0 00:00:00 sh
5744 pts/0 00:00:00 ps
>test1
>killed
after kill
>PID TTY TIME CMD
1580 pts/0 00:00:00 sudo
1581 pts/0 00:00:00 su
1589 pts/0 00:00:00 bash
5732 pts/0 00:00:00 parent
5737 pts/0 00:00:00 parent <defunct>
5753 pts/0 00:00:00 sh
5754 pts/0 00:00:00 ps
>test
> PID TTY TIME CMD
1580 pts/0 00:00:00 sudo
1581 pts/0 00:00:00 su
1589 pts/0 00:00:00 bash
5732 pts/0 00:00:00 parent
5737 pts/0 00:00:00 parent <defunct>
5759 pts/0 00:00:00 parent
5762 pts/0 00:00:00 sh
5763 pts/0 00:00:00 aplay
5765 pts/0 00:00:00 sh
5766 pts/0 00:00:00 ps
>test1
>killed
after kill
>PID TTY TIME CMD
1580 pts/0 00:00:00 sudo
1581 pts/0 00:00:00 su
1589 pts/0 00:00:00 bash
5732 pts/0 00:00:00 parent
5737 pts/0 00:00:00 parent <defunct>
5759 pts/0 00:00:00 parent <defunct>
5773 pts/0 00:00:00 sh
5774 pts/0 00:00:00 ps
i also tried with this in test() command
else if(pid1>0)
{
out("I AM IN PARENT PROCESS() NOW\n");
wait(&status);
}
by doing this only one parent process remain after aplay finishes to play sound, no matter how many times i call the "test" command. but the issue with this is that, I can not type any other command during sound play, until it finishes. so could not kill it in between playing the sound.
I want to play sound in parallel, and want to kill any time i want.
Don't kill it that way! You have the child PID in pid1, just use the kill() function to kill it.
Spawning an separate process to run killall is unnecessary, expensive and (as you've found out), not that reliable. What if there are five copies of that executable running?
You can use something like:
#include <sys/types.h>
#include <signal.h>
:
int rc = kill (pid1, 9); // or SIGKILL for portability, rather than 9.
// Check rc and errno.
in your parent.
I would also seriously look into removing the system calls in the child process as well since they start up separate processes. You can do better by using the exec family of calls the replace the program in the child's process space.

Resources